Skip to content

Commit

Permalink
增加server
Browse files Browse the repository at this point in the history
  • Loading branch information
Baiang committed Jul 19, 2018
1 parent 7b520e6 commit 448f97f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
8 changes: 8 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"watch": [
"server/**/*.ts"
],
"execMap": {
"ts": "ts-node --compilerOptions {\"\"\"module\"\"\":\"\"\"commonjs\"\"\"}"
}
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "next src",
"dev": "nodemon server/index.ts",
"dev:test": "nodemon server/index.ts",
"dev:eslint": "cross-env ANALYZE=ESLINT next src",
"build": "next build src",
"start": "next start src",
Expand Down Expand Up @@ -57,13 +58,17 @@
"jest": "22.0.1",
"jest-transform-stub": "^1.0.0",
"next-compose-plugins": "^2.1.1",
"nodemon": "^1.18.3",
"open-browser-webpack-plugin": "^0.0.5",
"postcss-import": "^11.1.0",
"postcss-url": "^7.3.2",
"react-addons-test-utils": "15.6.2",
"react-test-renderer": "16.2.0",
"styled-jsx-plugin-sass": "^0.2.4",
"styled-jsx-postcss": "^0.2.0",
"ts-node": "^7.0.0",
"typescript": "^2.9.2",
"typescript-babel-jest": "^1.0.5",
"url-loader": "^1.0.1",
"webpack-bundle-analyzer": "^2.13.1",
"webpack-bundle-size-analyzer": "^2.7.0"
Expand Down
37 changes: 37 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 conf = require('../src/next.config.js')
// console.log(111, conf)

const app = next({
dev,
dir:'./src'
})
const handle = app.getRequestHandler()



//console.log(app)

app.prepare()
.then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl

if (pathname === '/a') {
app.render(req, res, '/a', query)
} else if (pathname === '/b') {
app.render(req, res, '/b', query)
} else {
handle(req, res, parsedUrl)
}
})
.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})
26 changes: 26 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compileOnSave": false,
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"jsx": "preserve",
"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"
]
}
}
8 changes: 8 additions & 0 deletions tsconfig.server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "build/production-server/"
},
"include": ["server/**/*.ts"]
}

0 comments on commit 448f97f

Please sign in to comment.