简体   繁体   中英

TypeScript Integration with existing Node js express project

I Want to develop node js express application with typescript. To integrate typescript in node js I followed a few tutorials from online and it is working as expected. but .ts files converted .js and it is stored in dist folder but I don't want like that. can I run my project with only with .ts files instead of converting to .js files in dist folder?

Yes, have a look at ts-node

Install it globally:

npm install -g ts-node

Run app.ts:

ts-node app.ts

Answering your question in comments:

First run tsc --init to create tsconfig.json automatically. Then install npm i @types/node and npm i @types/express

In ./routes/index.ts:

import {Request, Response} from 'express'
export function Hello(req: Request ,res: Response) {
    res.send('Hello World')
}

In app.ts

import express from 'express'; 
const app = express(); 
import {Hello} from './routes/index' 
app.get('/', Hello); 
app.listen(3000, () => console.log('Example app listening on port 3000!'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM