Created
February 24, 2018 20:55
-
-
Save dmitry-saritasa/3edf14f63879f293c827d4edc3ec135a to your computer and use it in GitHub Desktop.
server.js
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 JSON_SERVER = require('json-server'); | |
const SERVER = JSON_SERVER.create(); | |
const MIDDLEWARES = JSON_SERVER.defaults(); | |
// YOUR SEED FILE | |
const seed = require('./db.json'); | |
// if your seedfile exports a function, you should run it to get the generated json object | |
// in this case inside db.json we can have dynamically generated entries | |
// const data = seed(); | |
const data = seed; | |
// pass the `data` json object to `json-server`'s router function to generate the necessary routes | |
const ROUTER = JSON_SERVER.router(data); | |
let isAuthenticated = false; | |
// it is recommended to use the bodyParser middleware before any other middleware in your application | |
SERVER.use(JSON_SERVER.bodyParser); | |
// PREFIX | |
const rewriterRules = { | |
'/api/v1/*': '/$1', | |
}; | |
SERVER.use(JSON_SERVER.rewriter(rewriterRules)); | |
// your Authentication middleware (simplified to check the variable directly | |
// feel free to go back to the function approach if your authentication checks are more complex | |
SERVER.use((req, res, next) => { | |
res.header('Access-Control-Allow-Origin', 'http://localhost:8080'); | |
if (isAuthenticated || req.url === '/auth/login') { | |
next(); | |
} else { | |
res.status(401).send('Unauthorized!'); | |
} | |
}); | |
// If you want to emulate 401 HTTP Response, i.e. auth token got expired | |
// then uncomment the code below and restart server.js | |
// SERVER.get('/users', (req, res) => { | |
// res.status(401).send({}); | |
// }); | |
// the logout endpoint | |
SERVER.post('/auth/login', (req, res) => { | |
// const data = require('./db.json'); | |
const user = data.users.find(user => user.email === req.body.email); | |
if (user) { | |
isAuthenticated = true; | |
res.status(200).send(user); | |
} else { | |
res.status(404).send({}); | |
} | |
}); | |
// the logout endpoint | |
SERVER.get('/logout', (req, res) => { | |
isAuthenticated = false; | |
res.status(200).send('logged out'); | |
}); | |
// middlewares required by json-server to run correctly | |
// this sets up the static serving, Gzip, logger, | |
// the internal json-server body-parser .. etc | |
SERVER.use(MIDDLEWARES); | |
/* | |
I am not sure why you are rewriting this route like this, | |
because this subtle rewrite changes how you get/patch a single resource | |
so instead of getting single resource you made it search for | |
all resources that has the provided `id`, thus, it returns | |
an array of resources which in your case will always be an array | |
with single resource, plus it will prevent patching "modifying" | |
all `/home/:id` resources. | |
anyways, I kept it as it is. | |
if you really know what you are doing here, ignore all the above, | |
otherwise, try to disable this middleware to know what I mean. | |
*/ | |
SERVER.use(JSON_SERVER.rewriter({ | |
'/home/:id': '/home?id=:id', | |
})); | |
// this is where `json-server`'s magic happens ;) | |
SERVER.use(ROUTER); | |
/* | |
start the application by listening to port 3000, | |
Although this won't print the nice starting message you see when | |
running `json-server` as CLI command, it still runs the app correctly. | |
*/ | |
SERVER.listen(3000, () => { | |
console.log('http://localhost:3000'); // eslint-disable-line no-console | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment