ExpressJS setup to serve transpiled TypeScript files is giving me trouble. Whenever I try to access /components/foo.js
, I keep getting a 404 error.
/* /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>
Need help with optimizing folder structure and setting up the server.ts file to successfully include transpiled .js files in my index.html file.
https://i.sstatic.net/0Kxdf.png
Question: How can I properly configure my folder structure and server.ts to include certain transpiled .js files in my index.html?
P.S.: Came across this SO question, tried their method but failed. Need clarity on what they're doing differently.