2017-03-16 36 views
0

我得到这个错误在Heroku的GET https://my-app.herokuapp.com/index.js 404 (Not Found)的Heroku无法找到我的index.js文件

我Procfile有这个在它:web: npm start

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

我的app文件夹在根级别包含包含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文件指向它,但我这样做时,它仍然不承认它。

有什么想法?

回答

0

这是对节点和/或服务器如何工作的误解。你应该能够去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

相关问题