【发布时间】:2021-08-11 17:37:14
【问题描述】:
我正在使用 typescript 编译和运行只提供 html 文件的快速服务器,但我在 app.js 的 chrome 中的网络选项卡下的响应中不断收到此错误:Uncaught SyntaxError: Unexpected token '<'
这是我的服务器代码:
import { config } from 'dotenv'
import path from 'path'
import express from 'express'
config()
const app = express()
app.use(express.static(path.resolve(__dirname, "dist")))
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "index.html"), (err) => {
if (err) {
res.status(404).send(err)
} else {
res.status(500).send(err)
}
})
})
const port = process.env.PORT
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})
这是我编译和运行代码的命令:
"scripts": {
"compile": "rimraf ./dist/ && tsc",
"copy": "copyfiles -u 1 ./public/index.html ./dist",
"start": "npm run compile && npm run copy && node dist"
},
这是我的 index.html 文件:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>TypeScript</title>
</head>
<body>
<script type="text/javascript" src="/app.js" defer></script>
</body>
</html>
知道我做错了什么吗?我很确定文件路径是正确的,但是我尝试了./app.js 并从<script> 标记中删除了defer 属性,但我仍然收到此错误。
【问题讨论】:
标签: html node.js typescript express