Creating a Node.js or Express server application using TypeScript involves several steps, from setting up the development environment to writing the code and running the server. Below is a step-by-step guide to help you create a basic Express server using TypeScript.
Step 1: Install Node.js and npm
Ensure you have Node.js and npm installed. You can download and install them from the official Node.js website.
To check if they are installed, run:
node -v
npm -v
Step 2: Initialize the Project
Create a new directory for your project and navigate into it. Then, initialize a new npm project:
mkdir my-typescript-express-app
cd my-typescript-express-app
npm init -y
Step 3: Install Dependencies
Install Express and TypeScript, along with some necessary TypeScript types and tools:
npm install express
npm install --save-dev typescript @types/node @types/express ts-node nodemon
Step 4: Configure TypeScript
Create a tsconfig.json
file to configure TypeScript. Run:
npx tsc --init
Modify tsconfig.json
to suit your needs. A basic configuration might look like this:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
Step 5: Create the Project Structure
Create the following directory and file structure:
my-typescript-express-app/
├── src/
│ ├── index.ts
├── package.json
├── tsconfig.json
Step 6: Write the Server Code
In src/index.ts
, write the basic code for an Express server:
import express, { Request, Response } from 'express';
const app = express();
const port = 3000;
app.get('/', (req: Request, res: Response) => {
res.send('Hello, TypeScript with Express!');
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
Step 7: Configure Scripts for Development
In your package.json
, add the following scripts to automate the build and run process:
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts"
}
Step 8: Run the Server
To run the server in development mode (with automatic restarts on file changes), use:
npm run dev
For a production build and run, use:
npm run build
npm start
Step 9: Test Your Server
Open a browser and navigate to http://localhost:3000
. You should see "Hello, TypeScript with Express!" displayed.
Optional: Linting and Formatting
To maintain code quality, you can add ESLint and Prettier to your project:
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin prettier eslint-config-prettier eslint-plugin-prettier
Create an .eslintrc.json
file:
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:prettier/recommended"
],
"rules": {
"prettier/prettier": ["error"]
}
}
Create a .prettierrc
file:
{
"singleQuote": true,
"trailingComma": "all"
}
Add linting and formatting scripts to package.json
:
"scripts": {
"lint": "eslint 'src/**/*.{ts,js}'",
"format": "prettier --write 'src/**/*.{ts,js}'"
}
Run the linting and formatting:
npm run lint
npm run format
That's it! You now have a basic Node.js Express server running with TypeScript. You can expand this by adding routes, middleware, and other functionalities as needed.