From cd0e13df01bd04875a5b303301c54680ca72209d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Xalambr=C3=AD?= Date: Sat, 2 Dec 2017 20:44:38 -0300 Subject: [PATCH] Add example of Nodemon on a custom server (#3374) --- examples/custom-server-nodemon/README.md | 29 +++++++++++++++++++ examples/custom-server-nodemon/nodemon.json | 3 ++ examples/custom-server-nodemon/package.json | 15 ++++++++++ examples/custom-server-nodemon/pages/a.js | 3 ++ examples/custom-server-nodemon/pages/b.js | 3 ++ examples/custom-server-nodemon/pages/index.js | 9 ++++++ .../custom-server-nodemon/server/index.js | 28 ++++++++++++++++++ 7 files changed, 90 insertions(+) create mode 100644 examples/custom-server-nodemon/README.md create mode 100644 examples/custom-server-nodemon/nodemon.json create mode 100644 examples/custom-server-nodemon/package.json create mode 100644 examples/custom-server-nodemon/pages/a.js create mode 100644 examples/custom-server-nodemon/pages/b.js create mode 100644 examples/custom-server-nodemon/pages/index.js create mode 100644 examples/custom-server-nodemon/server/index.js diff --git a/examples/custom-server-nodemon/README.md b/examples/custom-server-nodemon/README.md new file mode 100644 index 00000000..91ed60e1 --- /dev/null +++ b/examples/custom-server-nodemon/README.md @@ -0,0 +1,29 @@ +[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/custom-server-nodemon) + +# Custom server with Nodemon example + +## How to use + +Download the example [or clone the repo](https://github.com/zeit/next.js): + +```bash +curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/custom-server-nodemon +cd custom-server-nodemon +``` + +Install it and run: + +```bash +npm install +npm run dev +``` + +Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) + +```bash +now +``` + +## The idea behind the example + +The example shows how you can apply [Nodemon](https://nodemon.io/) to a custom server to have live reload of the server code without being affected by the Next.js universal code. diff --git a/examples/custom-server-nodemon/nodemon.json b/examples/custom-server-nodemon/nodemon.json new file mode 100644 index 00000000..ab8591fb --- /dev/null +++ b/examples/custom-server-nodemon/nodemon.json @@ -0,0 +1,3 @@ +{ + "watch": ["server/**/*.js"] +} diff --git a/examples/custom-server-nodemon/package.json b/examples/custom-server-nodemon/package.json new file mode 100644 index 00000000..5d1aa0c0 --- /dev/null +++ b/examples/custom-server-nodemon/package.json @@ -0,0 +1,15 @@ +{ + "scripts": { + "dev": "nodemon server/index.js", + "build": "next build", + "start": "NODE_ENV=production node server/index.js" + }, + "dependencies": { + "next": "^4.1.4", + "react": "^16.2.0", + "react-dom": "^16.2.0" + }, + "devDependencies": { + "nodemon": "^1.12.1" + } +} diff --git a/examples/custom-server-nodemon/pages/a.js b/examples/custom-server-nodemon/pages/a.js new file mode 100644 index 00000000..c5359797 --- /dev/null +++ b/examples/custom-server-nodemon/pages/a.js @@ -0,0 +1,3 @@ +import React from 'react' + +export default () =>
a
diff --git a/examples/custom-server-nodemon/pages/b.js b/examples/custom-server-nodemon/pages/b.js new file mode 100644 index 00000000..9bde4d9d --- /dev/null +++ b/examples/custom-server-nodemon/pages/b.js @@ -0,0 +1,3 @@ +import React from 'react' + +export default () =>
b
diff --git a/examples/custom-server-nodemon/pages/index.js b/examples/custom-server-nodemon/pages/index.js new file mode 100644 index 00000000..d044fc1e --- /dev/null +++ b/examples/custom-server-nodemon/pages/index.js @@ -0,0 +1,9 @@ +import React from 'react' +import Link from 'next/link' + +export default () => ( + +) diff --git a/examples/custom-server-nodemon/server/index.js b/examples/custom-server-nodemon/server/index.js new file mode 100644 index 00000000..56cda752 --- /dev/null +++ b/examples/custom-server-nodemon/server/index.js @@ -0,0 +1,28 @@ +const { createServer } = require('http') +const { parse } = require('url') +const next = require('next') + +const port = parseInt(process.env.PORT, 10) || 3000 +const dev = process.env.NODE_ENV !== 'production' +const app = next({ dev }) +const handle = app.getRequestHandler() + +app.prepare() +.then(() => { + createServer((req, res) => { + const parsedUrl = parse(req.url, true) + const { pathname, query } = parsedUrl + + if (pathname === '/a') { + app.render(req, res, '/b', query) + } else if (pathname === '/b') { + app.render(req, res, '/a', query) + } else { + handle(req, res, parsedUrl) + } + }) + .listen(port, (err) => { + if (err) throw err + console.log(`> Ready on http://localhost:${port}`) + }) +})