Skip to content

Instantly share code, notes, and snippets.

@chris001177
Created June 22, 2019 19:35
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 chris001177/3b9042accd69e44bf0b078853c16d140 to your computer and use it in GitHub Desktop.
Save chris001177/3b9042accd69e44bf0b078853c16d140 to your computer and use it in GitHub Desktop.
const express = require('express')
const bodyParser = require('body-parser')
const jwt = require('express-jwt')
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
// authentication middleware
const auth = jwt({secret: 'somesuperdupersecret'})
app.get('/comments', (req, res) => {
res.json('All comments')
})
// secured endpoint
app.post('/comments/create', auth, (req, res) => {
// return error if user is not authenticated
if (!req.user){
return res.status(401).send('You are not authorized')
}
// create comment
const comment = {...}
res.json({
message: 'Comment created!',
data: comment
})
})
app.listen(3000, () => {
console.log('Server is up on 3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment