66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
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..');
|