create an express server in typescript

To create an Express server in TypeScript, follow these steps:

  1. Install dependencies:
index.ts
npm install express @types/express typescript ts-node-dev
58 chars
2 lines
  1. Create a tsconfig.json file:
index.ts
{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "./dist",
    "sourceMap": true
  },
  "include": ["./src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}
200 chars
11 lines
  1. Create a src directory and add a server.ts file inside it:
index.ts
import express from 'express';

const app = express();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});
196 chars
12 lines
  1. Add a start script to your package.json file:
index.ts
"scripts": {
  "start": "ts-node-dev --respawn --transpileOnly ./src/server.ts"
}
82 chars
4 lines
  1. Run the server:
index.ts
npm start
10 chars
2 lines

Now you have an Express server running in TypeScript!

gistlibby LogSnag