html - 如何将静态 HTML 和 CSS 文件连接到 Node.js 应用程序?

标签 html css node.js heroku

我尝试通过 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 () {

});

最佳答案

你应该使用:

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

关于html - 如何将静态 HTML 和 CSS 文件连接到 Node.js 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34416868/

相关文章:

node.js - 当我点击 github 中的 merge 请求按钮时,预 merge 提交钩子(Hook)是否会被执行?

javascript - 类型错误 : Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script

html - Flexbox 的麻烦

javascript - JQuery 的问题-切换多个图像

html - 如何将具有图标的导航居中,以便文本与图标对齐?

html - 将文本置于响应式图像中居中

jquery - 如何使用CSS动画水平然后垂直移动div?

node.js - 如何使用 npm 和 nvm 安装 node-sass?

javascript - 如何添加另一个相同的下拉列表?

html - css 优先级 : overriding only one of height and width