我正在尝试设置 ExpressJS 以从目录中提供转译的 TypeScript 文件,以便我可以将它们包含在我的index.html
. /components/foo.js
运行此设置时,我不断收到 404 。
/* /dist/server.js: */
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static('public'));
app.use('/components', express.static(path.join(__dirname, '/dist/components')));
var options = {
root: __dirname + '/dist/components/',
};
app.get('/', function(req: any, res: any) {
res.sendFile('/index.html', options);
});
app.listen(8090);
<!-- /public/index.html -->
<html>
<head>
<title>demo</title>
<script src="/components/foo.js" type="module"></script>
</head>
<body>
<h1>hello</h1>
</body>
</html>
这是我的文件夹结构:
问题:设置文件夹结构的最明智的方法是什么server.ts
,以便能够.js
在我的index.html
.
PS:我发现了这个 SO question并尝试了他们方法的变体,但没有奏效:我不明白他们在做什么,所以我在黑暗中拍摄。
设置文件夹结构和 server.ts 以便能够
.js
在我的.ts 文件中包含某些转译的最明智的方法是什么index.html
?
我会将包含转译代码的文件夹static
提供为virtual path
.
您可以提供components
文件夹,假设它包含可以安全公开的转译代码
app.use('/static', express.static(__dirname + '/components')); // server.js and components are in the same folder
// Then in your html you could simply, notice the absolute path
<script src="/static/foo.js" type="module"></script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句