【问题标题】:Display basic HTML file in NodeJS and ExpressJS在 NodeJS 和 ExpressJS 中显示基本 HTML 文件
【发布时间】:2013-03-09 22:41:34
【问题描述】:

我正在使用 NodeJS 和 ExpressJS 为简单的后端实现 RESTful API 服务器。我也在使用 BackboneJS 在前面渲染视图。因此,现在我有一个 index.html 文件,我想在客户端向“/”路由发送 /GET 时呈现该文件。这是我目前所拥有的:

var express = require('express');
var app = express();
app.use(express.bodyParser());
var mongo = require('mongodb');
var mongoose = require('mongoose');

var Server = mongo.Server, 
    DB = mongo.Db,
    BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});


db = new DB('mydb', server, {safe: true});

db.open(function(err, db) {
    if(!err) {
        console.log("Connected to 'mydb' database");
        db.collection('items', {safe:true}, function(err, collection) {
            if (err) {
                console.log("Creating collection 'items'");
            }
        });
    }
});

var port = process.env.PORT || 3000;

app.engine('.html');

var listItems = function(req, res){
    db.collection('items', function(err, collection){
        var items = collection.find();
        console.log(items);
        res.send(items);
    });
}

var itemDetails = function(req, res){

}

var deleteItem = function(req, res){

}

var createItem = function(req, res){

}

var updateItem = function(req, res){

}

var routeHome = function(req, res){

        // What can I do here to render a plain .html file?

}

app.all('*', function(req, res, next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Content-Type", "application/json");
    next();
});

app.get('/', routeHome);

app.get('/items', listItems);
app.get('/items/:id', itemDetails);
app.del('/users/:id', deleteItem);
app.post('/users', createItem);
app.put('/users/:id', updateItem);

app.listen(port);

console.log("Server started up on port " + port);

如您所见,我已经完成了所有设置,只是我不知道如何将常规 .html 文件发送到客户端。我不需要渲染引擎,因为 Backbone 正在为我做这一切。我只是想让 index.html 去客户端。

【问题讨论】:

    标签: node.js backbone.js express


    【解决方案1】:

    只使用express.static 中间件怎么样?由于您使用的是 Backbone,您可能还需要发送静态 JS 文件:

    app.use(express.static(__dirname));
    

    将它放在其他路线之前的某个地方。它将尝试查找__dirname(您的 app.js 文件所在的目录,但您当然可以更改中间件尝试查找文件的位置)中请求的文件,包括index.html

    【讨论】:

    • 那么我会在我的 app.get() 回调函数中添加什么来将该文件发送回客户端?
    • 没什么,你甚至可以删除它:)
    • 那么您在上面所做的将允许 GET 到 '/'(我网站的根目录)自动呈现 index.html?
    • 正确。以及您可能希望从 index.html 加载的任何 js/css 文件。
    【解决方案2】:

    第一步,删除app.engine(".html")

    第二步:

    var routeHome = function(req, res){
        // Do What Ever
        res.sendFile("index.html",function(err){ // Transfer The File With COntent Type Text/HTML
            if(err){
                res.end("Sorry, Error.");
            }else{
                res.end(); // Send The Response
            }
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 2012-09-06
      • 2012-09-09
      相关资源
      最近更新 更多