feat(express-post-cors): initial commit
This commit is contained in:
parent
eb1aaa2e08
commit
9a455ea7bc
|
@ -0,0 +1,65 @@
|
||||||
|
const debug=require('debug')('debug');
|
||||||
|
|
||||||
|
//start
|
||||||
|
debug('api start...');
|
||||||
|
|
||||||
|
/*Express makes it easy to register route handlers for POST requests. Here's a basic POST request handler.*/
|
||||||
|
const express = require('express');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
//TODO make this list available via config
|
||||||
|
//limit access to this origin list
|
||||||
|
let whitelist = [
|
||||||
|
'http(s)://foo.bar',
|
||||||
|
'http://localhost:8080'
|
||||||
|
];
|
||||||
|
|
||||||
|
//configure CORS
|
||||||
|
const cors = require("cors");
|
||||||
|
app.use(cors({
|
||||||
|
origin: function(origin, callback){
|
||||||
|
// allow requests with no origin
|
||||||
|
debug('origin: '+origin)
|
||||||
|
if(!origin){
|
||||||
|
return callback(null, true);
|
||||||
|
}
|
||||||
|
if(whitelist.indexOf(origin) === -1){
|
||||||
|
let message = 'The cors policy for this origin does not allow access from the particular origin: '+origin;
|
||||||
|
return callback(new Error(message), false);
|
||||||
|
}
|
||||||
|
debug('origin: '+origin+' allowed by cors');
|
||||||
|
return callback(null, true);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Parse JSON bodies for this app. Make sure you put
|
||||||
|
// `app.use(express.json())` **before** your route handlers!
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
/*
|
||||||
|
app.post('/', function requestHandler(req, res) {
|
||||||
|
console.log('post() start...');
|
||||||
|
res.end('Hello, World!');
|
||||||
|
console.log('post() done.');
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
app.post('*', (req, res) => {
|
||||||
|
console.log('post() req.body: ' + JSON.stringify(req.body));
|
||||||
|
//const answerBody = { answer: 42};
|
||||||
|
//req.body; // JavaScript object containing the parsed JSON
|
||||||
|
//res.json(req.body);
|
||||||
|
//res.json(answerBody);
|
||||||
|
res.sendStatus(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
const server = app.listen(3000, 'localhost', () => {
|
||||||
|
const host = server.address().address;
|
||||||
|
const port = server.address().port;
|
||||||
|
|
||||||
|
console.log('listen() running at http://%s:%s', host, port);
|
||||||
|
});
|
||||||
|
|
||||||
|
//end
|
||||||
|
debug('api done..');
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"name": "express-post-cors",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "Stefan Begerad",
|
||||||
|
"license": "GPL-3.0-or-later",
|
||||||
|
"dependencies": {
|
||||||
|
"cors": "2.8.5",
|
||||||
|
"debug": "^4.3.4",
|
||||||
|
"express": "4.18.2"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue