如何将静态HTML和CSS文件连接到Node.js应用程序?
我试图通过Heroku显示(静态)HTML网页。我遵循了本教程:但经过多次尝试,它仍然不起作用 我对编码相当陌生,所以如果你能给出具体的例子,那就太好了 以下文件已推送到heroku:如何将静态HTML和CSS文件连接到Node.js应用程序?,html,css,node.js,heroku,Html,Css,Node.js,Heroku,我试图通过Heroku显示(静态)HTML网页。我遵循了本教程:但经过多次尝试,它仍然不起作用 我对编码相当陌生,所以如果你能给出具体的例子,那就太好了 以下文件已推送到heroku: server.js package.json Procfile.js (folder) public with index.html, main.css //Server.js文件: var express = require('express'); //require express module in s
server.js
package.json
Procfile.js
(folder) public with index.html, main.css
//Server.js文件:
var express = require('express'); //require express module in server.js file
var app = express();
var mongojs = require('mongojs');
var db = mongojs('birthdaylist', ['birthdaylist']);
var bodyParser = require('body-parser');
var http = require('http');
var port = Number(process.env.PORT || 3000);
app.use(express.static(__dirname + '/public')); //connect to html file
app.use(bodyParser.json());
app.get('/birthdaylist', function(req, res) {
console.log("The server has received a GET request.")
db.birthdaylist.find(function(err, docs){
console.log(docs);
res.json(docs);
});
});
app.post('/birthdaylist', function(req, res){
console.log(req.body);
db.birthdaylist.insert(req.body, function (err, doc){
res.json(doc);
});
});
app.delete('/birthdaylist/:id', function(req, res){
var id = req.params.id;
console.log(id);
db.birthdaylist.remove({_id: mongojs.ObjectId(id)}, function(err, doc){
res.json(doc);
});
});
app.listen(port, function () {
});
你应使用:
app.listen(%PORT_NUMBER%, function () {
// some code here
});
而不是:
var server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type':'text/html'});
res.end('<h6>Hello worldsfasfd!</h6>');
});
var server=http.createServer(函数(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.end('Hello worldsfasfd!');
});
它起作用了,但当我刷新页面时,出现了一个应用程序错误。我已经更新了我的代码(见上文)。我试图访问(如果你访问该站点,你会看到错误)。heroku部署期间似乎出现了问题。你有任何错误吗?尝试查看日志。我能找到的唯一错误是“NPM_CONFIG_LOGLEVEL=error”它在本地工作吗?你在代码中更新了什么?尝试重新部署。