mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
* Add custom-server-typescript example (see #3694) * Fix linting errors in custom-server-typescript * Provide proper arguments to ts-node. * Fix import and fix all linting errors. * Use import in server as well. * Update nodemon.json
This commit is contained in:
parent
5362cbe116
commit
99fb191286
40
examples/custom-server-typescript/README.md
Normal file
40
examples/custom-server-typescript/README.md
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
[![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-typescript)
|
||||||
|
|
||||||
|
# Custom server with TypeScript + Nodemon example
|
||||||
|
|
||||||
|
## How to use
|
||||||
|
|
||||||
|
### Using `create-next-app`
|
||||||
|
|
||||||
|
Download [`create-next-app`](https://github.com/segmentio/create-next-app) to bootstrap the example:
|
||||||
|
|
||||||
|
```
|
||||||
|
npm i -g create-next-app
|
||||||
|
create-next-app --example custom-server-typescript custom-server-typescript-app
|
||||||
|
```
|
||||||
|
|
||||||
|
### Download manually
|
||||||
|
|
||||||
|
Download the example [or clone the repo](https://github.com/zeit/next.js):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/custom-server-typescript
|
||||||
|
cd custom-server-typescript
|
||||||
|
```
|
||||||
|
|
||||||
|
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 use [TypeScript](https://typescriptlang.com) on both the server and the client while using [Nodemon](https://nodemon.io/) to live reload the server code without affecting the Next.js universal code.
|
2
examples/custom-server-typescript/next.config.js
Normal file
2
examples/custom-server-typescript/next.config.js
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
const withTypescript = require('@zeit/next-typescript')
|
||||||
|
module.exports = withTypescript()
|
6
examples/custom-server-typescript/nodemon.json
Normal file
6
examples/custom-server-typescript/nodemon.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"watch": ["server/**/*.ts"],
|
||||||
|
"execMap": {
|
||||||
|
"ts": "ts-node --compilerOptions '{\"module\":\"commonjs\"}'"
|
||||||
|
}
|
||||||
|
}
|
20
examples/custom-server-typescript/package.json
Normal file
20
examples/custom-server-typescript/package.json
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"scripts": {
|
||||||
|
"dev": "nodemon server/index.ts",
|
||||||
|
"build": "next build && tsc --module commonjs",
|
||||||
|
"start": "NODE_ENV=production node lib/index.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"next": "latest",
|
||||||
|
"react": "^16.2.0",
|
||||||
|
"react-dom": "^16.2.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/next": "^2.4.7",
|
||||||
|
"@types/react": "^16.0.36",
|
||||||
|
"@zeit/next-typescript": "^0.0.8",
|
||||||
|
"nodemon": "^1.12.1",
|
||||||
|
"ts-node": "^4.1.0",
|
||||||
|
"typescript": "^2.7.1"
|
||||||
|
}
|
||||||
|
}
|
3
examples/custom-server-typescript/pages/a.tsx
Normal file
3
examples/custom-server-typescript/pages/a.tsx
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export default () => <div>a</div>
|
3
examples/custom-server-typescript/pages/b.tsx
Normal file
3
examples/custom-server-typescript/pages/b.tsx
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export default () => <div>b</div>
|
9
examples/custom-server-typescript/pages/index.tsx
Normal file
9
examples/custom-server-typescript/pages/index.tsx
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-typescript/server/index.ts
Normal file
28
examples/custom-server-typescript/server/index.ts
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import { createServer } from 'http'
|
||||||
|
import { parse } from 'url'
|
||||||
|
import * as next from '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}`)
|
||||||
|
})
|
||||||
|
})
|
29
examples/custom-server-typescript/tsconfig.json
Normal file
29
examples/custom-server-typescript/tsconfig.json
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
{
|
||||||
|
"compileOnSave": false,
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "esnext",
|
||||||
|
"module": "esnext",
|
||||||
|
"allowJs": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"removeComments": false,
|
||||||
|
"preserveConstEnums": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"baseUrl": ".",
|
||||||
|
"typeRoots": [
|
||||||
|
"./node_modules/@types"
|
||||||
|
],
|
||||||
|
"lib": [
|
||||||
|
"dom",
|
||||||
|
"es2015",
|
||||||
|
"es2016"
|
||||||
|
],
|
||||||
|
"outDir": "lib/"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"server/**/*.ts"
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in a new issue