简体   繁体   English

TypeScript 与现有 Node js express 项目的集成

[英]TypeScript Integration with existing Node js express project

I Want to develop node js express application with typescript.我想用打字稿开发 node js express 应用程序。 To integrate typescript in node js I followed a few tutorials from online and it is working as expected.为了在 node js 中集成打字稿,我从网上学习了一些教程,它按预期工作。 but .ts files converted .js and it is stored in dist folder but I don't want like that.但是 .ts 文件转换为 .js 并存储在 dist 文件夹中,但我不想那样。 can I run my project with only with .ts files instead of converting to .js files in dist folder?我可以只使用 .ts 文件运行我的项目而不是转换为 dist 文件夹中的 .js 文件吗?

Yes, have a look at ts-node是的,看看ts-node

Install it globally:全局安装:

npm install -g ts-node

Run app.ts:运行 app.ts:

ts-node app.ts

Answering your question in comments:在评论中回答您的问题:

First run tsc --init to create tsconfig.json automatically.首先运行tsc --init tsconfig.json自动创建tsconfig.json Then install npm i @types/node and npm i @types/express然后安装npm i @types/nodenpm i @types/express

In ./routes/index.ts:在 ./routes/index.ts 中:

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

In app.ts在 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!'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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