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 41
| var express = require('express'); var https = require('https'); var http = require('http'); var fs = require('fs'); var bodyParser = require('body-parser');
// This line is from the Node.js HTTPS documentation. var options = { key: fs.readFileSync('qmsauthn.key.pem'), cert: fs.readFileSync('qmsauthn.cert.pem') };
// Create a service (the app object is just a callback). var app = express();
app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); // in latest body-parser use like below. app.use(bodyParser.urlencoded({ extended: true }));
// get api app.get('/api/v1', (req, res) => { res.status(200).send({ success: 'true', message: 'todos retrieved successfully', }) });
// post api (Content-Type: application/x-www-form-urlencoded) app.post('/api/v2', (req, res) => { console.log(req.body); res.status(200).send({ success: 'true', message: 'todos retrieved successfully', }) });
// Create an HTTP service. http.createServer(app).listen(80); // Create an HTTPS service identical to the HTTP service. https.createServer(options, app).listen(443);
|