历史/背景:显示node.js的html页面
问题陈述:数据库已连接,但当我在本地主机中运行时,它显示'无法获取'。
预期结果:它应该从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>
答案 0 :(得分:1)
这里有2个错误:
您应该从root&#39; /&#39;定义路线。这意味着,如果你想打印&#34; hi&#34;在127.0.0.1:7550/hi你会写:
app.get(&#39; / hi&#39;,function(req,res){ res.send(&#39;喜&#39); });
要提供静态文件,您应该使用express.static中间件。
app.use(express.static(__ dirname +&#39; / public&#39;));
尝试生成示例快速应用程序并查看其结构:https://github.com/expressjs/generator