Skip to content

Instantly share code, notes, and snippets.

@blogcacanid
Created November 12, 2020 07:49
Show Gist options
  • Save blogcacanid/f9e87a950122b896eec4dd48df9a7f22 to your computer and use it in GitHub Desktop.
Save blogcacanid/f9e87a950122b896eec4dd48df9a7f22 to your computer and use it in GitHub Desktop.
server.js Authentication JWT Node.js
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
// CORS multiple Domain
var allowlist = [
'http://localhost:3000',
'http://localhost:4200',
'http://localhost:8080'
]
var corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (allowlist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
} else {
corsOptions = { origin: false } // disable CORS for this request
}
callback(null, corsOptions) // callback expects two parameters: error and options
}
app.use(cors(corsOptionsDelegate));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to Authentication JWT Node.js." });
});
// routes
require('./app/routes/auth.routes')(app);
require('./app/routes/user.routes')(app);
// set port, listen for requests
const PORT = process.env.PORT || 9090;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment