Created
December 4, 2020 15:51
-
-
Save dmitriyzyuzin/580986a6683d0bfe94221309eae023d2 to your computer and use it in GitHub Desktop.
Simple NodeJS Express server with CORS and pre-flyght
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const cors = require('cors') | |
const bodyParser = require("body-parser") | |
const app = express() | |
const PORT = process.env.port || 4000 | |
// to parse req.body for POST-requests | |
app.use(bodyParser.urlencoded({extended: true})); | |
app.use(bodyParser.json()); | |
app.options('/i-eat-params/', cors()) | |
app.del('/i-eat-params/', cors(), function (req, res, next) { | |
res.json({msg: 'This is CORS-enabled for all origins!'}) | |
}) | |
app.post('/i-eat-params/', cors(), function (req, res, next) { | |
const parsedParams = JSON.stringify(req.body, null, 2) | |
console.log("parsedParams: ", parsedParams) | |
res.json({status: 'success'}) | |
}) | |
app.listen(PORT, function () { | |
console.log(`CORS-enabled web server listening on port ${PORT}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run server:
node server.js --port 4000
Simple check: