app.js 885 B

12345678910111213141516171819202122232425262728293031
  1. const express = require('express');
  2. const port = 3002;
  3. const bodyParser = require('body-parser');
  4. const routes = require('./routes/routes')
  5. const app = express();
  6. // Use Node.js body parsing middleware
  7. app.use(bodyParser.json());
  8. app.use(bodyParser.urlencoded({
  9. extended: true,
  10. }));
  11. app.use((req, res, next) => {
  12. res.header('Access-Control-Allow-Origin', '*');
  13. res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  14. next();
  15. app.options('*', (req, res) => {
  16. // allowed XHR methods
  17. res.header('Access-Control-Allow-Methods', 'GET, PATCH, PUT, POST, DELETE, OPTIONS');
  18. res.send();
  19. });
  20. });
  21. routes(app);
  22. // Start the server
  23. const server = app.listen(port, (error) => {
  24. if (error) return console.log(`Error: ${error}`);
  25. console.log(`Server listening on port ${server.address().port}`);
  26. });