Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@malihassan20
Last active February 3, 2018 13:59
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 malihassan20/d1c9880cdaf65d074c24b2f1dff58b90 to your computer and use it in GitHub Desktop.
Save malihassan20/d1c9880cdaf65d074c24b2f1dff58b90 to your computer and use it in GitHub Desktop.
nextJs-tour-diary-app\server.js
const next = require('next');
const routes = require('./routes');
const { createServer } = require('http');
const { parse } = require('url');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const handler = routes.getRequestHandler(app);
app.prepare().then(() => {
createServer(handler, (req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
if (pathname === '/tour-detail/:tourId') {
app.render(req, res, '/tour-detail', query);
} else {
handle(req, res, parsedUrl);
}
}).listen(process.env.PORT || 3000, err => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment