Skip to content

Instantly share code, notes, and snippets.

@robertklep
Created April 20, 2019 05:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertklep/d4095a127671ee2da65c046208bed8ad to your computer and use it in GitHub Desktop.
Save robertklep/d4095a127671ee2da65c046208bed8ad to your computer and use it in GitHub Desktop.
const express = require('express');
const app = express();
const server = app.listen(3012);
app.get('/', (req, res) => {
res.send('open route');
});
const router = express.Router();
router.get('/', (req, res) => {
res.send('protected route');
});
router.use((req, res, next) => {
res.status(404).send('protected route not found');
})
app.use('/protected', (req, res, next) => {
if (! req.headers.authorization) {
return res.status(403).json({ error: 'No credentials sent!' });
}
next();
}, router);
app.use((req, res, next) => {
res.status(404).send('open route not found');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment