9951 explained code solutions for 126 technologies


expressjsHow can I use Express.js with TypeScript?


Express.js is a popular Node.js web application framework that can be used with TypeScript for creating web applications.

To use Express.js with TypeScript, you need to first install the necessary packages:

npm install express @types/express

Then, create a TypeScript file, such as app.ts:

import express from 'express';

const app = express();

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

app.listen(3000, () => {
  console.log('Server started!');
});

Finally, compile the TypeScript file to JavaScript and run it:

tsc app.ts
node app.js

Output example

Server started!

The code above is a basic example of how to use Express.js with TypeScript. It creates a web server that listens for requests on port 3000 and responds with "Hello World" when a request is made to the root path.

More information about using Express.js with TypeScript can be found in the official Express.js documentation and the TypeScript Express tutorial.

Edit this code on GitHub