【问题标题】:Heroku cant find my index.js fileHeroku 找不到我的 index.js 文件
【发布时间】:2017-08-07 09:47:09
【问题描述】:

我在 heroku GET https://my-app.herokuapp.com/index.js 404 (Not Found) 中收到此错误

我的 Procfile 中有这个:web: npm start

然后在我的 package.json 我有"start": "node ./app/index.js"

我的根级别的应用程序文件夹包含一个index.js,其中包含:

var express = require('express');
var app = express();

app.use('/', express.static('public'));

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/index.html');
});

app.listen(process.env.PORT || 8080);

而我的webpack.config.js 文件包含:

var path = require('path');
const webpack = require('webpack')

module.exports = {
 entry: './main.js',

 output: {
   path: path.resolve(__dirname, 'public'),
   filename: 'index.js'
 },

 module: {
   loaders: [
     {
       test: /\.js$/,
       exclude: /node_modules/,
       loader: 'babel',
       query: {
         presets: ['es2015', 'react', 'stage-2']
       }
     },
     {
        test: /\.scss$/,
        loaders: ["style-loader", "css-loader", "sass-loader"]
      }
   ]
 }
}

也许我需要将它指向我的 public/index.js 文件,但是当我这样做时它仍然无法识别它。

有什么想法吗?

【问题讨论】:

    标签: javascript node.js heroku webpack


    【解决方案1】:

    这是对节点和/或服务器的一般工作方式的误解。您应该可以访问GET https://my-app.herokuapp.com/,它会返回您的公共文件夹中的index.html 文件。告诉节点以"start": "node ./app/index.js" 开始仅仅意味着运行这个文件来启动服务器。服务器会监听你定义的路由:

    app.get('/', function(req, res) {
        res.sendFile(__dirname + '/index.html');
    });
    

    这是在https://my-app.herokuapp.com/路径上监听

    如果你想听https://my-app.herokuapp.com/anotherroute,你可以改变之前的:

    app.get('/anotherroute', function(req, res) {
        res.sendFile(__dirname + '/index.html');
    });
    

    并且由于您使用了app.use('/', express.static('public'));,任何与您的路线匹配的文件都将被自动提供。这意味着如果您的public 目录中有一个文件,例如apple.jpg,您可以使用https://my-app.herokuapp.com/apple.jpg 检索它

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 2018-09-02
      • 2021-07-13
      • 2016-12-25
      • 2021-08-17
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 2019-04-01
      相关资源
      最近更新 更多