Last active
November 7, 2017 19:42
-
-
Save thaerlabs/95e02aba6d2315dcfda2304ef03d8edb to your computer and use it in GitHub Desktop.
express sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
const PORT = 3000; | |
app.use(bodyParser.json()); | |
app.get('/', (req, res) => { | |
const name = req.query; | |
res.send('Login form'); | |
}); | |
app.post('/register', (req, res) => { | |
const body = req.body; | |
// get the data, validate and save the user in the database | |
res.send(body); | |
}); | |
app.post('/login', (req, res) => { | |
const body = req.body; | |
// get the data, validate and uathneticate the user, return the toker | |
res.send({ | |
token: 'fake token' | |
}); | |
}); | |
app.get('/private', (req, res) => { | |
// if not authenticated redirect to login | |
// else show something | |
}); | |
app.listen(PORT, () => { | |
console.log(`Server running at http://localhost:${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment