Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //middleware
- app.use('/', (req, res, next) => {
- if (req.method === 'GET' || req.method === 'POST') {
- return next(req, res);
- } else {
- return res.status(400).send('Only POST and GET method allowed');
- }
- });
- //GET endpoint
- app.get('/', (req, res) => {
- const ua = req.headers['user-agent'];
- res.render('index', { //index is html template
- ua,
- time: Date.now()
- });
- });
- //POST endpoint
- app.post('/', (req, res) => {
- const {echo} = req.body;
- if (echo) {
- const ua = req.headers['user-agent'];
- res.render('index', {
- ua,
- time: Date.now(),
- echo
- });
- } else {
- res.status(400).send('\"echo\" field is missing');
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement