Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // server.js
- // BASE SETUP
- // =============================================================================
- // call the packages we need
- var express = require('express'); // call express
- var Sequelize = require('sequelize');
- var cors = require('cors');
- var config = require('./ressources/config.json')
- var app = express(); // define our app using express
- var bodyParser = require('body-parser');
- var password = config.password ? config.password : null;
- /*var sequelize = new Sequelize(
- config.database,
- config.user,
- config.password, {
- host: config.host,
- dialect: config.dialect,
- logging: console.log,
- define: {
- timestamps: false
- }
- }
- ); */
- // configure app to use bodyParser()
- // this will let us get the data from a POST
- app.use(bodyParser.urlencoded({ extended: true }));
- app.use(bodyParser.json());
- var port = process.env.PORT || 8080; // set our port
- // ROUTES FOR OUR API
- // =============================================================================
- var router = express.Router(); // get an instance of the express Router
- var humainModel = require('./app/models/humain');
- // more routes for our API will happen here
- // REGISTER OUR ROUTES -------------------------------
- // all of our routes will be prefixed with /api
- app.use('/api', router, function(req,res,next)
- {
- // Website you wish to allow to connect
- res.writeHead({'Access-Control-Allow-Origin': 'http://localhost:8100'});
- // Request methods you wish to allow
- res.writeHead('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
- // Request headers you wish to allow
- res.writeHead('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
- // Set to true if you need the website to include cookies in the requests sent
- // to the API (e.g. in case you use sessions)
- res.writeHead('Access-Control-Allow-Credentials', true);
- res.writeHead(200, {"Content-Type": "application/json"});
- // Pass to next layer of middleware
- next();
- }/*,cors({origin: 'http://localhost:8100'})*/);
- // test route to make sure everything is working (accessed at GET http://localhost:8080/api)
- router.get('/', function (req, res) {
- humainModel.findAll()
- .then(function(humain){
- res.end(JSON.stringify(humain));
- },function(error) {
- res.status(500).send(error);
- });
- // res.json({ message: 'hooray! welcome to our api!' });
- });
- // START THE SERVER
- // =============================================================================
- app.listen(port);
- console.log('Magic happens on port ' + port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement