Created
May 16, 2018 23:12
-
-
Save stramel/7f2b66a51b5557d8f5d00f2f4a7078b7 to your computer and use it in GitHub Desktop.
Next.js Express Server with next-routes
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 next = require('next') | |
const cookieParser = require('cookie-parser') | |
const routes = require('../universal/routes') | |
const dev = process.env.NODE_ENV !== 'production' | |
const app = next({ dev }) | |
const handle = app.getRequestHandler() | |
const routeHandler = routes.getRequestHandler(app) | |
const server = express() | |
const port = 3000 | |
module.exports = app | |
.prepare() | |
.then(() => | |
server | |
.use(bodyParser.json()) | |
.use(bodyParser.urlencoded({ extended: true })) | |
.use(cookieParser()) | |
.use(routeHandler) | |
.get('*', (req, res) => handle(req, res)), | |
) | |
.then(() => server.listen(port, () => console.log(`> Ready on http://localhost:${port}`))) | |
.catch(err => { | |
console.error(err) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment