Skip to content

Instantly share code, notes, and snippets.

@kamiyam
Last active April 20, 2018 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiyam/996023564d4d2cc04334b1197c693c36 to your computer and use it in GitHub Desktop.
Save kamiyam/996023564d4d2cc04334b1197c693c36 to your computer and use it in GitHub Desktop.
migrate express js2ts
  • code 一式

https://github.com/kamiyam/typed-express-sample

  • init
mkdir project
cd project
npx express-generator src
mv src/package.json package.json
npm i -D typescript tslint ts-node @types/node @types/express @types/cookie-parser @types/http-errors @types/debug

npx tsc --init
npx tslint --init

mv src/app.js src/app.ts
mv src/routes/index.js src/routes/index.ts
mv src/routes/users.js src/routes/users.ts
mv src/bin/www src/bin/www.ts

mkdir dist

cp src/public dist/public
cp src/views dist/views
  • edit package.json
"scripts": {
	"start": "PORT=3333 node ./dist/bin/www",
	"serve": "ts-node ./src/bin/www.ts",	
	"build": "tsc",
	"lint": "tslint src/**/*.ts"
 }
  • edit tsconfig.json
{
	"compilerOptions": 
	{ 
		"target": “ES2017”,
		"outDir": "./dist",
		"lib": [
			"es6"
		],
		"sourceMap": true,
		"rootDir": "./src"
	},
	"include": [
		"src/**/*"
	],
	"exclude": [
		"node_modules"
	]
}
  • replace code
import express from 'express';
// import {default as express, Request, Response, NextFunction} from 'express';
function onError(error: any) {
app.use(function(req: express.Request, res: express.Response, next: express.NextFunction) {
function normalizePort(val: any): number|string|boolean {
  • build
npm run build
  • server
npm run serve
# access http://localhost:3000

npm run build
npm start 
# access http://localhost:3333
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment