【发布时间】:2020-05-03 08:17:31
【问题描述】:
自从开发以来,我已经转了很多圈,现在我有点迷茫,代码一团糟。这是 我的第一个节点项目(在框架之外),我开始希望我使用 PHP。无论如何,这里是 webpack.common.js 文件。
const path = require('path');
require("babel-polyfill");
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
app: ['babel-polyfill','./src/index.js'],
},
plugins: [
new HtmlWebpackPlugin({
title: 'Production',
filename: 'index.html',
template: './src/index.html',
}),
],
output: {
publicPath: '/',
filename: 'js/bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use:{
loader: 'babel-loader'
}
},
{
test: /\.css$/,
exclude: /node_modules/,
use: ['style-loader', 'css-loader'],
},
]
},
};
When I push to Heroku master I set NPM_CONFIG_PRODUCTION is 'false' so that dev dependencies also get
loaded. Here is the package.json so you know what I'm using and what I've tried to some extent.
{
"name": "stock",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server --mode development --open",
"start": "node backendindex.js",
"heroku-postbuild": "webpack -p"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.7.5",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.7.6",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.6",
"babel-plugin-transform-regenerator": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^3.4.0",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^1.0.2",
"webpack": "^4.41.3",
"webpack-dev-server": "^3.10.0",
"webpack-merge": "^4.2.2"
},
"dependencies": {
"@babel/runtime": "^7.8.3",
"axios": "^0.19.0",
"bootstrap": "^4.4.1",
"core-js": "^3.6.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet-csp": "^2.9.4",
"jquery": "^1.12.4",
"mysql": "^2.17.1",
"popper.js": "^1.16.0",
"regenerator-runtime": "^0.13.3",
"serve-favicon": "^2.5.0",
"webpack-cli": "^3.3.10"
},
"engines": {
"node": "12.13.1",
"npm": "6.12.1"
}
}
下一点我有点不确定什么时候上线,因为我还没有测试它,但它似乎是 正确的。这是 db 调用的搜索类。
import axios from 'axios';
"use strict";
export default class Router{
constructor(query){
this.query = query;
}
async getResults(){
try{
const res = await axios(`https://stockapp.herokuapp.com/api/stockapi/${this.query}`);
this.result = res;
//console.log(this.result);
}catch(error){
alert(error);
}
}
}
Finally here are my express settings.
app.use(express.static(__dirname + '/dist'));
app.use(express.json());
app.use('/api/stockapi', apiRouter);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});
app.get('*', (req, res) => {
const index = path.join(__dirname, 'dist', 'index.html');
res.sendFile(index);
});
因此,一旦将 index.html 推送到 Heroku,就会提供 index.html 并且看起来不错,然后当我以 一旦它想返回再生器运行时异步,它就会给我这个错误。我已经尝试了一切! 帮助我OB1,你是我唯一的希望!!!
【问题讨论】:
标签: javascript node.js express heroku webpack