9951 explained code solutions for 126 technologies


expressjsHow can I create a boilerplate project with Express.js and TypeScript?


Creating a boilerplate project with Express.js and TypeScript is easy.

  1. First, create a new project directory and navigate into it:
mkdir my-project
cd my-project
  1. Install the npm packages for Express.js and TypeScript:
npm install express typescript
  1. Create a tsconfig.json file to configure the TypeScript compiler:
{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "strict": true
  }
}
  1. Create a src directory and an index.ts file to contain the Express application code:
mkdir src
touch src/index.ts
  1. In the index.ts file, add the code for the Express application:
import express from "express";

const app = express();

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

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});
  1. Compile the TypeScript code to JavaScript:
tsc
  1. Finally, start the Express server:
node dist/index.js

Output example

Server listening on port 3000

Helpful links

Edit this code on GitHub