Node / express

时间:2017-08-02 18:32:38

标签: node.js express ejs

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 index = require('./routes/index');
var newproject = require('./routes/newProject');

var app = express();



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

// 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('/', index);
app.use('/newproject', newproject);



// 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 handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

index.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
    res.render('index');
});

module.exports = router;

newproject .js

var express = require('express');
var router = express.Router();
var sql = require ('mssql');

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


module.exports = router;

newProject.ejs

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
hello
</body>
</html>

和视图中类似的index.ejs。

我的主目录工作正常,localhost:3000/可以工作,但是当我输入时 localhost:3000/newproject它出现以下错误:

  

错误:无法在视图目录“F:\ project \ pcgWebApp \ views”中查找视图“错误”           在EventEmitter.render(F:\ project \ pcgWebApp \ node_modules \ express \ lib \ application.js:580:17)           在ServerResponse.render(F:\ project \ pcgWebApp \ node_modules \ express \ lib \ response.js:971:7)           在F:\ project \ pcgWebApp \ app.js:47:7           在Layer.handle_error(F:\ project \ pcgWebApp \ node_modules \ express \ lib \ router \ layer.js:71:5)           在trim_prefix(F:\ project \ pcgWebApp \ node_modules \ express \ lib \ router \ index.js:315:13)           在F:\ project \ pcgWebApp \ node_modules \ express \ lib \ router \ index.js:284:7           在Function.process_params(F:\ project \ pcgWebApp \ node_modules \ express \ lib \ router \ index.js:335:12)           在下一个(F:\ project \ pcgWebApp \ node_modules \ express \ lib \ router \ index.js:275:10)           在F:\ project \ pcgWebApp \ app.js:36:3           在Layer.handle [as handle_request](F:\ project \ pcgWebApp \ node_modules \ express \ lib \ router \ layer.js:95:5)

任何人都知道发生了什么 - ?请帮忙 - 尝试过安装npm。

如果我放置

,它确实有用
router.get('/newproject', function(req, res) {
    res.render('newProject');
});
在app.js中

并将路由器更改为app.get ..

2 个答案:

答案 0 :(得分:0)

newProject.js中更改此内容:

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

为此:

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

由于newProject.js似乎被用作外部路由器,因此当您编写代码时,您的最终端点将是:

  

本地主机:3000 / newproject / newproject

相反,你希望它是:

  

本地主机:3000 / newproject /

这就是为什么你应该在你的路线中指定'/'作为终点的原因:因为Express中的路径路径是关联的

答案 1 :(得分:0)

我相信您的第一个错误源于此部分代码:

app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

更具体地说,是尝试呈现error视图的部分。我会确保您的视图文件夹中有error.ejs个文件/视图。

我还会将您的新project.js文件中的/newproject根目录更改为/,因为根目录来自文件名。