Skip to content

Instantly share code, notes, and snippets.

@roelvan
Created November 26, 2018 11:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roelvan/034dc9ac570f32021e76f0637e8d46a3 to your computer and use it in GitHub Desktop.
Save roelvan/034dc9ac570f32021e76f0637e8d46a3 to your computer and use it in GitHub Desktop.
NOW v2 Local DEV env Node.js
require('dotenv').config();
const http = require('http');
const url = require('url');
// import NOW SETTINGS
const now = require('./now.json');
const PORT = process.env.PORT || 3030;
const routes = now.routes.reduce((map, route) => {
map[route.src] = require('.' + route.dest);
return map;
}, {});
http
.createServer(async (req, res) => {
// filter out any query strings
const { pathname } = url.parse(req.url);
try {
await routes[pathname](req, res);
} catch (err) {
res.writeHead(404);
res.end();
}
})
.listen(PORT, () => console.log(`👂 Listening: http://localhost:${PORT}`));
{
"routes": [
{
"src": "/auth/local",
"methods": ["POST"],
"dest": "/api/auth/local/index.js"
},
{
"src": "/auth/google",
"methods": ["GET"],
"dest": "/api/auth/google/index.js"
},
{
"src": "/auth/google/callback",
"methods": ["GET"],
"dest": "/api/auth/google/callback.js"
},
{ "src": "/games", "methods": ["GET"], "dest": "/api/games/index.js" }
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment