【问题标题】:How to connect static HTML and CSS files to Node.js application?如何将静态 HTML 和 CSS 文件连接到 Node.js 应用程序?
【发布时间】:2015-12-22 21:54:21
【问题描述】:

我尝试通过 Heroku 显示一个(静态)HTML 网页。我已按照本教程进行操作:https://www.youtube.com/watch?v=gAwH1kSODVQ 但经过多次尝试后仍然无法正常工作。

我对编码比较陌生,所以如果你能给出具体的例子那就太好了!

以下文件已推送到heroku:

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 () {

});

【问题讨论】:

    标签: html css node.js heroku


    【解决方案1】:

    你应该使用:

    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>');
    });
    

    【讨论】:

    • 它工作了,但是当我刷新页面时,我得到一个应用程序错误。我已经更新了我的代码(见上文)。
    • 我尝试访问tue2id60.herokuapp.com(如果您访问该站点,您会看到错误”)
    • heroku 部署过程中似乎出现了问题。你有什么错误吗?尝试查看日志
    • 我能找到的唯一错误是“NPM_CONFIG_LOGLEVEL = error”
    • 它在本地工作吗?您在代码中究竟更新了什么?尝试重新部署。
    猜你喜欢
    • 1970-01-01
    • 2019-03-06
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2021-10-27
    • 1970-01-01
    • 2017-04-17
    相关资源
    最近更新 更多