Skip to content

Instantly share code, notes, and snippets.

@desaijay315
Created March 29, 2019 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desaijay315/fc9a5709ed6ce27c42d9ede70efb260d to your computer and use it in GitHub Desktop.
Save desaijay315/fc9a5709ed6ce27c42d9ede70efb260d to your computer and use it in GitHub Desktop.
const cookieParser = require('cookie-parser');
const csrf = require('csurf');
const bodyParser = require('body-parser');
const express = require('express');
const csrfProtection = csrf({ cookie: true });
const parseForm = bodyParser.urlencoded({ extended: false });
const app = express();
// parse cookies
// we need this because "cookie" is true in csrfProtection
app.use(cookieParser());
app.get('/form', csrfProtection, function (req, res) {
// pass the csrfToken to the view
res.render('send', { csrfToken: req.csrfToken() })
})
app.post('/process', parseForm, csrfProtection, function (req, res) {
res.send('data is being processed')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment