Skip to content

Instantly share code, notes, and snippets.

@dmitriyzyuzin
Created December 4, 2020 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitriyzyuzin/580986a6683d0bfe94221309eae023d2 to your computer and use it in GitHub Desktop.
Save dmitriyzyuzin/580986a6683d0bfe94221309eae023d2 to your computer and use it in GitHub Desktop.
Simple NodeJS Express server with CORS and pre-flyght
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}`)
})
@dmitriyzyuzin
Copy link
Author

How to run server: node server.js --port 4000
Simple check:

curl 'http://localhost:4000/i-eat-params/'  --data-binary '{"param1":143,"param2":{"width":300,"height":150},"param3":{}}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment