如何解决如何使用webpack在不同的端口上运行两个单独的文件?
有两个入口点。 main.js index.js 并且有两个html文件
main.html index.html
构建时,我有js文件连接到HTML。 index.js链接到index.html等。
任务是在package.json文件中创建两个命令
开始索引-starts index.html file on port 3000
启动主程序 runs main.html file on port 8080
每个人都必须在不同端口上使用不同的连接文件运行不同的HTML。
请告诉我该怎么做?
解决方法
据我了解,类似的东西可能会有所帮助-我不确定为什么您需要两个js文件,您可能只是像express这样的框架,而只是在不同的端口上提供不同的文件。让我知道这是否是您想要的!
const express = require('express');
const path = require('path');
const app = express();
const secondaryApp = express();
const PRIMARY_PORT = process.env.PRIMARY_PORT || 3000;
const SECONDARY_PORT = process.env.SECONDARY_PORT || 8000;
const STATIC = path.join(__dirname,'../static/');
const HTML_FILE_PRIMARY = path.join(STATIC,'index.html');
const HTML_FILE_SECONDARY = path.join(STATIC,'index2.html');
app.use(express.static(STATIC));
secondaryApp.use(express.static(STATIC));
// Add routes to both apps
app.post('/route',function(req,res) {
res.status(200).end();
});
secondaryApp.post('/route',res) {
res.status(200).end();
});
// Set up both GET requests to index
app.get('/',(req,res) => {
res.sendFile(HTML_FILE_PRIMARY);
})
secondaryApp.get('/',res) => {
res.sendFile(HTML_FILE_SECONDARY);
})
// Let both servers listen on their own ports
app.listen(PRIMARY_PORT,() => {
console.log("Primary app listening on port: "+PRIMARY_PORT);
})
secondaryApp.listen(SECONDARY_PORT,() => {
console.log("Secondary app listening on port: "+SECONDARY_PORT);
});
,
您有很多选择可以实现这一目标
- 一致link
用法
npm i concurrently
并在package.json中
"start": "concurrently \"command1 arg\" \"command2 arg\""
- 您可以使用
&
运算符进行管理
使用
"dev": "npm run firstarg & npm run secondarg"
这可能会对您有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。