Skip to content

Instantly share code, notes, and snippets.

@pravin772
Last active August 5, 2020 02:01
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 pravin772/d96b9572c9b2e4080450f7234b9ac22e to your computer and use it in GitHub Desktop.
Save pravin772/d96b9572c9b2e4080450f7234b9ac22e to your computer and use it in GitHub Desktop.
Node.js Express server
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser');
const app = express()
app.use(cors())
app.use(express.json())
app.use(bodyParser.json())
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*") // update to match the domain you will make the request from
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE') // If needed
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
res.header('Access-Control-Allow-Credentials', true)
next()
});
app.use('/protected/', require('./routes/index'))
app.listen('5000', () => console.log('Server started and listening on PORT 5000'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment