Advertisement
Guest User

server.js

a guest
Feb 17th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // server.js
  2.  
  3. // BASE SETUP
  4. // =============================================================================
  5.  
  6. // call the packages we need
  7. var express = require('express');        // call express
  8. var Sequelize = require('sequelize');
  9. var cors = require('cors');
  10. var config = require('./ressources/config.json')
  11. var app = express();                 // define our app using express
  12. var bodyParser = require('body-parser');
  13.  
  14. var password = config.password ? config.password : null;
  15.  
  16. /*var sequelize = new Sequelize(  
  17.     config.database,  
  18.     config.user,
  19.     config.password, {  
  20.         host: config.host,
  21.         dialect: config.dialect,
  22.         logging: console.log,  
  23.         define: {  
  24.             timestamps: false  
  25.         }  
  26.     }  
  27. );  */
  28.  
  29.  
  30. // configure app to use bodyParser()
  31. // this will let us get the data from a POST
  32. app.use(bodyParser.urlencoded({ extended: true }));
  33. app.use(bodyParser.json());
  34.  
  35. var port = process.env.PORT || 8080;        // set our port
  36.  
  37. // ROUTES FOR OUR API
  38. // =============================================================================
  39. var router = express.Router();              // get an instance of the express Router
  40.  
  41.  
  42.  
  43.  
  44. var humainModel = require('./app/models/humain');
  45. // more routes for our API will happen here
  46.  
  47. // REGISTER OUR ROUTES -------------------------------
  48. // all of our routes will be prefixed with /api
  49. app.use('/api', router, function(req,res,next)
  50. {
  51.     // Website you wish to allow to connect
  52.     res.writeHead({'Access-Control-Allow-Origin': 'http://localhost:8100'});
  53.  
  54.     // Request methods you wish to allow
  55.     res.writeHead('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  56.  
  57.     // Request headers you wish to allow
  58.     res.writeHead('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
  59.  
  60.     // Set to true if you need the website to include cookies in the requests sent
  61.     // to the API (e.g. in case you use sessions)
  62.     res.writeHead('Access-Control-Allow-Credentials', true);
  63.  
  64.     res.writeHead(200, {"Content-Type": "application/json"});
  65.     // Pass to next layer of middleware
  66.     next();
  67. }/*,cors({origin: 'http://localhost:8100'})*/);
  68.  
  69. // test route to make sure everything is working (accessed at GET http://localhost:8080/api)
  70. router.get('/', function (req, res) {
  71.     humainModel.findAll()
  72.         .then(function(humain){
  73.            
  74.             res.end(JSON.stringify(humain));
  75.         },function(error) {
  76.             res.status(500).send(error);
  77.         });
  78.    // res.json({ message: 'hooray! welcome to our api!' });
  79. });
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. // START THE SERVER
  89. // =============================================================================
  90. app.listen(port);
  91. console.log('Magic happens on port ' + port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement