mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Add example of Nodemon on a custom server (#3374)
This commit is contained in:
parent
36f6179a52
commit
cd0e13df01
29
examples/custom-server-nodemon/README.md
Normal file
29
examples/custom-server-nodemon/README.md
Normal file
|
@ -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.
|
3
examples/custom-server-nodemon/nodemon.json
Normal file
3
examples/custom-server-nodemon/nodemon.json
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"watch": ["server/**/*.js"]
|
||||||
|
}
|
15
examples/custom-server-nodemon/package.json
Normal file
15
examples/custom-server-nodemon/package.json
Normal file
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
3
examples/custom-server-nodemon/pages/a.js
Normal file
3
examples/custom-server-nodemon/pages/a.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export default () => <div>a</div>
|
3
examples/custom-server-nodemon/pages/b.js
Normal file
3
examples/custom-server-nodemon/pages/b.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export default () => <div>b</div>
|
9
examples/custom-server-nodemon/pages/index.js
Normal file
9
examples/custom-server-nodemon/pages/index.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import React from 'react'
|
||||||
|
import Link from 'next/link'
|
||||||
|
|
||||||
|
export default () => (
|
||||||
|
<ul>
|
||||||
|
<li><Link href='/b' as='/a'><a>a</a></Link></li>
|
||||||
|
<li><Link href='/a' as='/b'><a>b</a></Link></li>
|
||||||
|
</ul>
|
||||||
|
)
|
28
examples/custom-server-nodemon/server/index.js
Normal file
28
examples/custom-server-nodemon/server/index.js
Normal file
|
@ -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}`)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in a new issue