Advertisement
fakesamgregory

Basic Node Server

Jun 12th, 2021 (edited)
1,626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3.  
  4. app.use(express.urlencoded({ extended: true }));
  5. app.use(express.json());
  6.  
  7. app.use(function(req, res, next) {
  8.   res.header("Access-Control-Allow-Origin", "*");
  9.   res.header("Access-Control-Allow-Headers", "Content-Type,x-xsrf-token,x-requested-with");
  10.   res.header("Access-Control-Allow-Methods", "GET,PUT,POST,PATCH,DELETE");
  11.   next();
  12. })
  13.  
  14. app.get('/success',function(req,res){
  15.   console.log(req.query);
  16.   res.status(200).send({message: "Hello " + req.query.name});
  17. });
  18.  
  19. app.get('/fail', function(req, res) {
  20.   res.status(400).send({message: 'You failed'});
  21. });
  22.  
  23. app.post('/post', function(req, res) {
  24.   console.log(req.body);
  25.   res.status(200).send({message: 'Success'});
  26. });
  27.  
  28. app.post('/', function(req, res) {
  29.   if (!req.body.name) {
  30.     res.status(400).send({message: "You did not provide a name"});
  31.   } else {
  32.     res.status(200).send({message: "Thank you!"});
  33.   }
  34. });
  35.  
  36. var port = 80;
  37.  
  38. app.listen(port);
  39.  
  40. console.log('Listening on port', port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement