簡體   English   中英

Heroku找不到我的index.js文件

[英]Heroku cant find my index.js file

我在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文件,但是當我這樣做時,它仍然無法識別它。

有任何想法嗎?

這是對節點和/或服務器通常如何工作的誤解。 您應該可以轉到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進行檢索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM