Create a gist now

Instantly share code, notes, and snippets.

@kamiyam /README.md
Last active Apr 20, 2018

Embed
What would you like to do?
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