Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. //middleware
  2. app.use('/', (req, res, next) => {
  3. if (req.method === 'GET' || req.method === 'POST') {
  4. return next(req, res);
  5. } else {
  6. return res.status(400).send('Only POST and GET method allowed');
  7. }
  8. });
  9.  
  10. //GET endpoint
  11. app.get('/', (req, res) => {
  12. const ua = req.headers['user-agent'];
  13. res.render('index', { //index is html template
  14. ua,
  15. time: Date.now()
  16. });
  17. });
  18.  
  19. //POST endpoint
  20. app.post('/', (req, res) => {
  21. const {echo} = req.body;
  22. if (echo) {
  23. const ua = req.headers['user-agent'];
  24. res.render('index', {
  25. ua,
  26. time: Date.now(),
  27. echo
  28. });
  29. } else {
  30. res.status(400).send('\"echo\" field is missing');
  31. }
  32. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement