Skip to content
Snippets Groups Projects
Unverified Commit ad4a1075 authored by Mauko Quiroga's avatar Mauko Quiroga
Browse files

Simplify express server

parent 4457d336
Branches
Tags
No related merge requests found
......@@ -32,6 +32,7 @@
"array-element-newline": ["error", "consistent"],
"func-style": ["error", "declaration"],
"fp/no-mutation": ["error", { "commonjs": true }],
"fp/no-unused-expression": "off",
"import/no-unresolved": "off",
"indent": ["error", 4],
"max-nested-callbacks": ["error", 1],
......
const express = require("express")
const path = require("path")
const cookieParser = require("cookie-parser")
const bodyParser = require("body-parser")
const next = require("./next")
const app = express()
// Put in place textbook middlewares for express.
if (process.env.NODE_ENV !== "production") {
// app.use(logger('dev'));
}
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use("/", express.static(path.join(__dirname, "public")))
const start = async (port) => {
// Couple Next.js with our express server.
// app and handle from "next" will now be available as req.app and req.handle.
await next(app)
// Normal routing, if you need it.
// Use your SSR logic here.
// Even if you don't do explicit routing the pages inside app/pages
// will still get rendered as per their normal route.
app.get("/main", (req, res) => req.app.render(req, res, "/", {
routeParam: req.params.routeParam,
}))
app.listen(port)
}
// Start the express server.
start(9001)
/* eslint-disable no-param-reassign */
const next = require("next")
const dev = process.env.NODE_ENV !== "production"
const app = next({ dev, dir: "./app" })
const handle = app.getRequestHandler()
module.exports = async (server) => {
await app.prepare()
// Middleware to insert app and handle inside the req object.
server.use("/", (req, res, n) => {
req.app = app
req.handle = handle
n()
})
server.originalListen = server.listen
server.listen = (port) => {
// If none of the custom routing handlers from express are hit,
// defer to next's own handler.
server.get("*", (req, res) => {
req.handle(req, res)
})
server.originalListen(port)
}
return server
}
......@@ -453,6 +453,7 @@
"version": "7.3.2",
"resolved": "https://registry.npmjs.org/@babel/plugin-proposal-pipeline-operator/-/plugin-proposal-pipeline-operator-7.3.2.tgz",
"integrity": "sha512-wuzx8U/KZLJYoqU6joiaKY0PixHuYZ3Vxys+wPahNAZEEm+EDb1eTc19DuJob3BdxYSD9PWPbwyoRbhkdoYErg==",
"dev": true,
"requires": {
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-syntax-pipeline-operator": "^7.3.0"
......@@ -556,6 +557,7 @@
"version": "7.3.0",
"resolved": "https://registry.npmjs.org/@babel/plugin-syntax-pipeline-operator/-/plugin-syntax-pipeline-operator-7.3.0.tgz",
"integrity": "sha512-LAa3ZcOAyfPOUDTp0W5EiXGSAFh1vz9sD8yY7sZzWzEkZdIC404pqBP60Yfu9GJDj0ggh+UTQY6EYlIDXVr0/Q==",
"dev": true,
"requires": {
"@babel/helper-plugin-utils": "^7.0.0"
}
......@@ -1985,6 +1987,7 @@
"version": "6.23.0",
"resolved": "https://registry.npmjs.org/babel-messages/-/babel-messages-6.23.0.tgz",
"integrity": "sha1-8830cDhYA1sqKVHG7F7fbGLyYw4=",
"dev": true,
"requires": {
"babel-runtime": "^6.22.0"
}
......@@ -1993,6 +1996,7 @@
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/babel-plugin-module-resolver/-/babel-plugin-module-resolver-3.2.0.tgz",
"integrity": "sha512-tjR0GvSndzPew/Iayf4uICWZqjBwnlMWjSx6brryfQ81F9rxBVqwDJtFCV8oOs0+vJeefK9TmdZtkIFdFe1UnA==",
"dev": true,
"requires": {
"find-babel-config": "^1.1.0",
"glob": "^7.1.2",
......@@ -2020,6 +2024,7 @@
"version": "6.24.1",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-strict-mode/-/babel-plugin-transform-strict-mode-6.24.1.tgz",
"integrity": "sha1-1fr3qleKZbvlkc9e2uBKDGcCB1g=",
"dev": true,
"requires": {
"babel-runtime": "^6.22.0",
"babel-types": "^6.24.1"
......@@ -2029,6 +2034,7 @@
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/babel-plugin-wrap-in-js/-/babel-plugin-wrap-in-js-1.1.1.tgz",
"integrity": "sha1-Q3qGqSwPGO8p6pIYTJeISNBICyA=",
"dev": true,
"requires": {
"babel-plugin-transform-strict-mode": "^6.6.5",
"babel-template": "^6.7.0"
......@@ -2059,6 +2065,7 @@
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-template/-/babel-template-6.26.0.tgz",
"integrity": "sha1-3gPi0WOWsGn0bdn/+FIfsaDjXgI=",
"dev": true,
"requires": {
"babel-runtime": "^6.26.0",
"babel-traverse": "^6.26.0",
......@@ -2071,6 +2078,7 @@
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-traverse/-/babel-traverse-6.26.0.tgz",
"integrity": "sha1-RqnL1+3MYsjlwGTi0tjQ9ANXZu4=",
"dev": true,
"requires": {
"babel-code-frame": "^6.26.0",
"babel-messages": "^6.23.0",
......@@ -2097,7 +2105,8 @@
"babylon": {
"version": "6.18.0",
"resolved": "https://registry.npmjs.org/babylon/-/babylon-6.18.0.tgz",
"integrity": "sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ=="
"integrity": "sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ==",
"dev": true
},
"balanced-match": {
"version": "1.0.0",
......@@ -5074,6 +5083,7 @@
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/find-babel-config/-/find-babel-config-1.2.0.tgz",
"integrity": "sha512-jB2CHJeqy6a820ssiqwrKMeyC6nNdmrcgkKWJWmpoxpE8RKciYJXCcXRq1h2AzCo5I5BJeN2tkGEO3hLTuePRA==",
"dev": true,
"requires": {
"json5": "^0.5.1",
"path-exists": "^3.0.0"
......@@ -5082,7 +5092,8 @@
"json5": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/json5/-/json5-0.5.1.tgz",
"integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE="
"integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=",
"dev": true
}
}
},
......@@ -6353,7 +6364,8 @@
"globals": {
"version": "9.18.0",
"resolved": "https://registry.npmjs.org/globals/-/globals-9.18.0.tgz",
"integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ=="
"integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==",
"dev": true
},
"globby": {
"version": "6.1.0",
......@@ -9437,6 +9449,7 @@
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/pkg-up/-/pkg-up-2.0.0.tgz",
"integrity": "sha1-yBmscoBZpGHKscOImivjxJoATX8=",
"dev": true,
"requires": {
"find-up": "^2.1.0"
}
......@@ -10626,7 +10639,8 @@
"reselect": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/reselect/-/reselect-3.0.1.tgz",
"integrity": "sha1-79qpjqdFEyTQkrKyFjpqHXqaIUc="
"integrity": "sha1-79qpjqdFEyTQkrKyFjpqHXqaIUc=",
"dev": true
},
"resolve": {
"version": "1.10.1",
......
......@@ -12,19 +12,16 @@
"license": "AGPL 3",
"scripts": {
"build": "cd app && next build",
"start": "npm run build && env NODE_ENV=production node app.js",
"dev": "nodemon --ignore app/ app.js",
"start": "npm run build && env NODE_ENV=production node server.js",
"dev": "nodemon --ignore app/ server.js",
"style:check": "eslint -c .eslintrc --ext .js,.jsx --ignore-path .gitignore .",
"style:format": "npm run style:check -- --fix",
"style:rules": "eslint --print-config .eslintrc"
},
"dependencies": {
"@babel/plugin-proposal-pipeline-operator": "^7.3.2",
"@material-ui/core": "^3.9.3",
"@zeit/next-sass": "^1.0.1",
"@zeit/next-typescript": "^1.1.1",
"babel-plugin-module-resolver": "^3.2.0",
"babel-plugin-wrap-in-js": "^1.1.1",
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.4",
"express": "^4.16.4",
......@@ -42,6 +39,9 @@
"react-plotly.js": "^2.3.0"
},
"devDependencies": {
"@babel/plugin-proposal-pipeline-operator": "^7.3.2",
"babel-plugin-module-resolver": "^3.2.0",
"babel-plugin-wrap-in-js": "^1.1.1",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
......
const next = require("next")
const express = require("express")
const bodyParser = require("body-parser")
const cookieParser = require("cookie-parser")
const path = require("path")
const dev = process.env.NODE_ENV !== "production"
const app = next({ dev, dir: "./app" })
const handle = app.getRequestHandler()
const port = parseInt(process.env.PORT, 10) || 9001
async function start() {
await app.prepare()
const server = express()
server.use(bodyParser.json())
server.use(bodyParser.urlencoded({ extended: false }))
server.use(cookieParser())
server.use("/", express.static(path.join(__dirname, "public")))
server.get("*", (req, res) => handle(req, res))
server.listen(port)
return server
}
start()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment