From 448f97f243b64e951b7ed4ab1bcb5d2466aaec5b Mon Sep 17 00:00:00 2001 From: Baiang <973366510@qq.com> Date: Thu, 19 Jul 2018 17:38:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodemon.json | 8 ++++++++ package.json | 7 ++++++- server/index.ts | 37 +++++++++++++++++++++++++++++++++++++ tsconfig.json | 26 ++++++++++++++++++++++++++ tsconfig.server.json | 8 ++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 nodemon.json create mode 100644 server/index.ts create mode 100644 tsconfig.json create mode 100644 tsconfig.server.json diff --git a/nodemon.json b/nodemon.json new file mode 100644 index 0000000..8b38780 --- /dev/null +++ b/nodemon.json @@ -0,0 +1,8 @@ +{ + "watch": [ + "server/**/*.ts" + ], + "execMap": { + "ts": "ts-node --compilerOptions {\"\"\"module\"\"\":\"\"\"commonjs\"\"\"}" + } +} diff --git a/package.json b/package.json index 1c2e4ab..cc2de14 100644 --- a/package.json +++ b/package.json @@ -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", @@ -57,6 +58,7 @@ "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", @@ -64,6 +66,9 @@ "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" diff --git a/server/index.ts b/server/index.ts new file mode 100644 index 0000000..5b1f99d --- /dev/null +++ b/server/index.ts @@ -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}`) + }) +}) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..41a1f0a --- /dev/null +++ b/tsconfig.json @@ -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" + ] + } +} diff --git a/tsconfig.server.json b/tsconfig.server.json new file mode 100644 index 0000000..314d705 --- /dev/null +++ b/tsconfig.server.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "build/production-server/" + }, + "include": ["server/**/*.ts"] +}