node.js无法启动 - 错误404 - 找不到

时间:2016-03-24 07:23:02

标签: javascript node.js express

我是node.js的新手,所以请不要再开始抨击我了。我正在使用快速框架,我正在尝试构建一个简单的应用程序以显示为html。

这是基本的app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//app.use('/', routes);
//app.use('/users', users);

app.use('/registration', require('./routes/registration'));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
  message: err.message,
  error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});

app.listen(3000, function () {
console.log("Poslusam na portu 3000!");
});

module.exports = app;

这是我试图显示的regisration.js代码

var express = require('express');
var router = express.Router();
var db = require('../node_modules/nyModule/db.js');

var app = express();

var users = db.getUsers();

router.get('/', function (req, res) {
res.render('registration');
});

router.post('/', function (req, res) {

var registration = JSON.stringify(req.body);
console.log(registration);

var o = JSON.parse(registration);

console.log("Login: " + o.email);

baza.addRegistration(o);
res.redirect('/');
});

module.exports = router;

,这里是registration.html

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h2>App</h2>
<h3>Registration/h3>
<form method="post">
    <label>Name: </label><br />
    <input type="text" name="name" id="name" /><br /><br />
    <label>LastName: </label><br />
    <input type="text" name="lname" id="lname" /><br /><br />
    <label>Elektronski naslov:</label><br />
    <input type="text" name="email" id="email" /><br /><br />
    <label>Username: </label><br>
    <input type="text" name="username" id="username" /><br>   <br>
    <label>Password</label><br>
    <input type="password" name="password" id="password" /><br>
    <input type="submit" value="Register" />
</form>
</body>
</html>

这是我获得localhost:3000

的结果

enter image description here

请帮帮我

更新

enter image description here

enter image description here

行:20

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该将视图引擎设置为html文件。

将此代码插入app.js文件。

app.set('views', __dirname + '/views'); 
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');