Last active
June 24, 2018 17:50
-
-
Save axelmarciano/56977855065f222147ef5f7df6644118 to your computer and use it in GitHub Desktop.
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
//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