我尝试使用node / express作为Web服务器创建用于学习目的的纯ES6应用程序。我正在使用模块导入,它在server.js文件中可以正常工作,但是当我在index.html中使用这些脚本时,他没有得到转换后的文件和调用&# 39; Uncaught SyntaxError:'我的问题是,如何从index.html访问转换后的文件?
server.js文件:
getSeverityColor(severity: number) {
switch (severity) {
case -1:
return "grey";
case 0:
return "green";
case 1:
return "yellow";
case 2:
return "orange";
case 3:
return "red";
default:
return "grey";
}
}
package.json文件:
import express from 'express'
const port = 3000
const app = express()
app.set("view options", {layout: false});
app.use(express.static(__dirname + '/src'));
app.get('/', function(req, res) {
res.render('index.html');
});
app.listen(port,()=>{
console.log("server running in port "+ port)
})
index.html文件:
{
"name": "ntn",
...
"scripts": {
"start": "nodemon server.js --exec babel-node"
},
"dependencies": {
"express": "^4.15.4",
"nodemon": "^1.12.1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-preset-env": "^1.6.0",
"babel-register": "^6.26.0",
"jasmine": "^2.8.0"
}
}
MemberController.js文件:
<!DOCTYPE html>
<html lang="en">
<head> ... </head>
<body>
<h1>Hello Word!</h1>
<script src="pages/member/MemberController.js"></script>
</body>
</html>
答案 0 :(得分:1)
您是否正在使用带有babel的webpack将ES6代码转换为ES5代码?一旦你设置了webpack,就会有一个名为&#34; babel-loader&#34;它为你做了改造。