我正在尝试使用express制作节点后端服务,我也想使用webpack将所有内容捆绑在单个文件中(不知道它是否有意义,我只是在学习)。我以这种方式设置:package.json
{
"name": "something",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js --mode=production",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-node-externals": "^1.7.2"
},
"dependencies": {
"express": "^4.17.1"
}
}
这是我的文件:webpack.config.js
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const webpack = require('webpack');
const backend = {
name: 'backend',
target: 'node',
devtool: 'source-map',
externals: [nodeExternals],
entry: path.resolve(__dirname, 'src/index.js'),
output: {
path: path.resolve(__dirname, 'bin'),
filename: 'app.js'
}
};
module.exports = [backend];
module.exports.plugins = [
new webpack.SourceMapDevToolPlugin({})
];
这是我的:src/index.js
var express = require('express');
var httpsrv = express();
httpsrv.get("/", function(res, rep) {
console.log("inside get.");
rep.send("<div>hey js!</div>");
});
httpsrv.listen(8080, function() {
console.log("server started.");
});
很基本,不是吗?当我构建并运行()应用程序时,一切都很好,它的行为符合预期,但有些事情听起来很奇怪。文件太短而无法捆绑库,当我将我的放入容器中时,我收到此错误:node ./bin/app.jsapp.jsexpressapp.jsnode:alpine
所以我没有得到我想要的:单个文件中的Web服务。这是怎么回事?
MM们
慕码人8056858
相关分类