【问题标题】:Can't load the html page from node.js无法从 node.js 加载 html 页面
【发布时间】:2014-09-26 21:14:00
【问题描述】:

历史/上下文:显示来自 node.js 的 html 页面

问题说明:数据库已连接,但是当我在本地主机上运行它时,它显示 'Cannot GET'

预期结果:它应该从 node.js 加载 html 页面

当前输出:数据库已连接但无法加载html页面

附上代码:

filter.js

var express = require('express'),
    filter = express(),
    server = require('http').createServer(filter),
    io = require('socket.io').listen(server),
    mongoose = require('mongoose');

server.listen(7550, '127.0.0.1');
console.log('Server running at http://127.0.0.1:7550/');

mongoose.connect('mongodb://localhost/filter_CheckBoxSchema', function (err) {
    if (err) {
        console.log('What happened!!!');
    } else {
        console.log('Connected to mongodb!');
    }
});

var filter_CheckBoxSchema = mongoose.Schema({
    category: { name: String, type: Boolean, default: false },
    created: { type: Date, default: Date.now }
});

var Filter = mongoose.model('Filter', filter_CheckBoxSchema);
filter.get('example.html', function (req, res) {    
    new Filter({
        name: req.body.name,
        type: req.body.gender,

    }).save(function (err, doc) {
        if (err) {
            throw err;
        }
        else
            res.send('Successfully inserted!!!');
    });
});

HTML

<html>
    <head>
        <title>
            Please enter your details
        </title>
    </head>
    <body>
        <h3>Please enter your details</h3>
        <p>Please register below!!!</p>

        <form action="filter.js" method="POST">
            Name: <input type="text" name="Name" />
            <br /><p></p>

            Gender:
            <br />
            <input type="radio" name="gender" /> Male
            <br />
            <input type="radio" name="gender" /> Female
            <p></p>
            Interest: (Check all that apply)
            <p>
            </p>
            <input type="checkbox" name="breakfast" /> Breakfast
            <br />
            <input type="checkbox" name="Lunch" /> Lunch
            <br />
            <input type="checkbox" name="Evening Snacks" /> Evening Snacks
            <br />
            <input type="checkbox" name="Dinner" /> Dinner
            <br />
            <p></p>
            <input type="submit" name="submit" value="Register!!!" />
        </form>
    </body>

</html>

【问题讨论】:

  • filter.get("example.html",...改为filter.get("/example.html",...
  • @fmodos: 对不起,先生仍然没有工作......
  • hmm...所以检查你的node.js服务器日志是否有错误,如果没有错误在node.js中放置一些console.log来检查函数是否被调用
  • 完成但同样的错误即将到来...我不知道该怎么办...请帮助...

标签: javascript html node.js mongodb


【解决方案1】:

这里有 2 个错误:

  1. 您应该从根“/”定义您的路线。这意味着如果你想在 127.0.0.1:7550/hi 上打印“hi”,你会写:

    app.get('/hi', function(req,res){ res.send('hi'); });

  2. 要提供静态文件,您应该使用 express.static 中间件。

    app.use(express.static(__dirname + '/public'));

尝试生成一个示例 express 应用程序,看看它的结构:https://github.com/expressjs/generator

【讨论】:

    猜你喜欢
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 2016-07-26
    • 2022-01-02
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多