首页 > 解决方案 > 使用 typescript 运行 node express 应用程序

问题描述

我创建了一个带有 typescript 支持的简单 node express 应用程序。有主要的index.tsx文件:

import express from 'express';
import cors from 'cors';
import GetDta from './src/discogs_api/services'

const app = express()
const port = 3000

app.use(cors());

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

app.get('/OAuth', (req, res) => {
    res.send('OAuth\'ing!...')
    GetDta()
})

app.listen(port, () => {
    console.log(`CORS Example app listening at http://localhost:${port}`)
})

现在,当我想使用以下方式运行应用程序时:

npx nodemon -e tsx index.tsx

我收到一个错误:Cannot use import statement outside a module.

当我将文件扩展名更改为ts我运行时没有任何问题时,更奇怪的是。为什么我不能使用tsx扩展?

标签: node.jstypescriptexpress

解决方案


推荐阅读