在后端使用網(wǎng)絡包
我正在嘗試使用express制作節(jié)點后端服務,我也想使用webpack將所有內(nèi)容捆綁在單個文件中(不知道它是否有意義,我只是在學習)。我以這種方式設置: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.jsconst 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.jsvar 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.");});很基本,不是嗎?當我構(gòu)建并運行()應用程序時,一切都很好,它的行為符合預期,但有些事情聽起來很奇怪。文件太短而無法捆綁庫,當我將我的放入容器中時,我收到此錯誤:node ./bin/app.jsapp.jsexpressapp.jsnode:alpine所以我沒有得到我想要的:單個文件中的Web服務。這是怎么回事?
查看完整描述