1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| const express = require('express'); const bodyParser = require('body-parser');
const app = express();
app.all('*', function (req, res, next) { res.header('Access-Control-Allow-Origin', req.headers.origin || '*'); res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.header('Access-Control-Allow-Credentials', true); res.header('Content-Type', 'application/json;charset=utf-8'); next(); });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/post/:name', function (req, res) { res.status(200).send({ status: 200, path: req.path, url: req.url, method: req.method, data: { body: req.body, query: req.query, querystring: req.originalUrl.split('?')[1], params: req.params }, msg: 'POST test' }); });
const server = app.listen(888, function () { let host = server.address().address; let port = server.address().port;
console.log('server:http://localhost:%s', port); });
|