这是我的webpack.config.js
const path = require("path");
//const merge = require("webpack-merge");
const htmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const htmlInlineCssPlugin = require("html-inline-css-webpack-plugin").default;
const htmlInlineSourcePlugin = require("html-webpack-inline-source-plugin");
var serverConfig = {
entry: "./src/server.js",
output: {
path: path.join(__dirname,'/bundle'),
filename: "server_bundle.js"
},
"target": "node",
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node-modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.hbs$/,
use: {
loader: "handlebars-loader"
}
}
]
},
devtool: "source-map",
plugins: [
new htmlWebpackPlugin({
inlineSource: '.(js|css)$'
}),
new htmlInlineSourcePlugin(),
]
}
module.exports = serverConfig;
这是我的server.js
let express = require("express");
let app = express();
//let Email = require("email-templates");
let path = require('path');
app.get("/getHandleBar",function(req,res){
var a = require( path.join(__dirname,"helpers","abc.hbs"));
res.send(a());
console.log(a());
})
app.listen(3000,function(){
console.log("app is listening on port 3000");
})
我正在访问时 http://localhost:3000/getHandleBar出现以下错误。实际上,我正在尝试使用webpack动态获取已编译的电子邮件模板。
SyntaxError: Unexpected token <
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at D:\nodeapp\src\server.js:39:13
at Layer.handle [as handle_request] (D:\nodeapp\node_modules\express\lib\router\layer.js:95:5)
at next (D:\nodeapp\node_modules\express\lib\router\route.js:137:13)
我的车把是
<html>
<head>
{{!-- <link rel="stylesheet" type="text/css" href="/helpers/abc.css"></link> --}}
</head>
<body>
<div>This is a</div>
</body>
</html>
这也是我的文件夹结构:请参阅屏幕截图 enter image description here
答案 0 :(得分:0)
绑定变量中注释的“开始”和“结束”标记缺少“ <”“>”。
{{!-- <link rel="stylesheet" type="text/css" href="/helpers/abc.css"></link> --}}
{{<!-- <link rel="stylesheet" type="text/css" href="/helpers/abc.css"></link> -->}}
答案 1 :(得分:0)
尝试使用res.sendFile()
代替res.send()
。
https://scotch.io/tutorials/use-expressjs-to-deliver-html-files
app.get("/getHandleBar",function(req,res){
res.sendFile(path.join(__dirname,"helpers","abc.hbs"));
})