https://github.com/d8corp/jap-express
Jap plugin for express
https://github.com/d8corp/jap-express
Last synced: about 1 year ago
JSON representation
Jap plugin for express
- Host: GitHub
- URL: https://github.com/d8corp/jap-express
- Owner: d8corp
- License: mit
- Created: 2019-10-19T22:30:59.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T05:18:05.000Z (over 3 years ago)
- Last Synced: 2025-03-17T03:15:50.186Z (over 1 year ago)
- Language: JavaScript
- Size: 488 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jap/express
[npm](https://www.npmjs.com/package/jap-express) | [github](https://github.com/d8corp/jap-express)
This is a plugin for [express](https://expressjs.com/) to use [jap](https://www.npmjs.com/package/jap).
> `japExpress(handler, resolve, reject, start, end, context)`
### Simple example
- Add file `index.js`
```javascript
const express = require('express')
const jap = require('jap-express')
const app = express()
app.get('/', (req, res) => res.send(''))
app.post('/', jap({test: true}))
app.listen(3000)
```
- Run the app
```bash
node index.js
```
- Open browser with `http://localhost:3000/` and run the next code in console
```javascript
fetch('/', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: '{"test": null}'
}).then(data => data.json()).then(console.log)
```
### Classic example
You may use classes with `jap`, this is a good way to have separately space for each request
- Add `classic.js`
```javascript
const express = require('express')
const jap = require('jap-express')
const app = express()
class App {
constructor (req, res) {
this.req = req
}
userAgent () {
return this.req.headers['user-agent']
}
}
app.get('/', (req, res) => res.send(''))
app.post('/', jap(App))
app.listen(3001)
```
- Run the app
```bash
node classic.js
```
- Open browser with `http://localhost:3001/` and run the next code in console
```javascript
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](https://www.npmjs.com/package/jap):
```bash
git clone https://github.com/d8corp/jap.git
cd jap
npm i
npm start
```