Skip to content

Instantly share code, notes, and snippets.

@axelmarciano
Last active June 24, 2018 17:50
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 axelmarciano/56977855065f222147ef5f7df6644118 to your computer and use it in GitHub Desktop.
Save axelmarciano/56977855065f222147ef5f7df6644118 to your computer and use it in GitHub Desktop.
//Définition des modules
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require('body-parser');
//Connexion à la base de donnée
mongoose.connect('mongodb://localhost/db').then(() => {
console.log('Connected to mongoDB')
}).catch(e => {
console.log('Error while DB connecting');
console.log(e);
});
//On définit notre objet express nommé app
const app = express();
//Body Parser
var urlencodedParser = bodyParser.urlencoded({
extended: true
});
app.use(urlencodedParser);
app.use(bodyParser.json());
//Définition des CORS
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
//Définition du routeur
var router = express.Router();
app.use('/user', router);
require(__dirname + '/controllers/userController')(router);
//Définition et mise en place du port d'écoute
var port = 8000;
app.listen(port, () => console.log(`Listening on port ${port}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment