1.2.0 • Published 6 years ago
jap-express v1.2.0
jap-express
npm | github
This is a plugin for express to jap your app.
Install with npm
npm i jap-expressInstall with yarn
yarn add jap-expressArguments
japExpress(handler: object | Jap,resolve?: (data: json) => json,reject?: (data: json, error: Error, handler) => json,start?: (req, res) => void,end?: (req, res) => void,context?: any
)
Simple example
- Add file
index.js
const express = require('express')
const jap = require('jap-express')
const app = express()
app.get('/', (req, res) => res.send(''))
app.post('/', jap({secret: p => p === 420 ? 42 : null}))
app.listen(3000)- Run the app
node index.js- Open browser with
http://localhost:3000/and run the next code in console
fetch('/', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: '{"secret": 420}'
}).then(data => data.json()).then(console.log)Jap
You may use Jap, this is a good way to have separately space for each request
- Add
classic.js
const express = require('express')
const jap, { Jap } = require('jap-express')
const app = express()
class App extends Jap {
userAgent () {
return this.$req.headers['user-agent']
}
}
app.get('/', (req, res) => res.send(''))
app.post('/', jap(App))
app.listen(3001)- Run the app
node classic.js- Open browser with
http://localhost:3001/and run the next code in console
fetch('/', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: '{"userAgent": null}'
}).then(data => data.json()).then(console.log)Real example
You may look at real example with jap:
git clone https://github.com/d8corp/jap.git
cd jap
npm i
npm start