节点js'TypeError:app.route不是函数' -

时间:2017-10-29 12:57:27

标签: javascript node.js

我收到以下错误:

olddognewtrix123:~/workspace (master) $ node server.js
/home/ubuntu/workspace/app/routes/index.js:7
        app.route('/')
            ^

TypeError: app.route is not a function

......并且不知道为什么。

我在c9.io中构建它

这是更新后的github repo:

https://github.com/olddognewtrix123/nodeJSvignettes

以下是我认为错误所在的代码:

服务器,JS:

 var express = require('express');

 var session = require('express-session');
 var path = require('path');
 var mongoose = require('mongoose');
 var port = process.env.PORT || 8080;
 var mongodb=require("mongodb")
 var MongoClient = mongodb.MongoClient;
 var routes = require('./app/routes/index.js');

 var app = express();

 app.use('/controllers', express.static(process.cwd() + '/app/controllers'));
 app.use('/public', express.static(process.cwd() + '/public'));


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

 routes(app);

 app.listen(port,  function () {
    console.log('Node.js listening on port ' + port + '...');
 });

index.js:

 'use strict';

 var path = process.cwd();

 module.exports = function (app){

    app.route('/')
        .get(function (req, res) {
            res.sendFile(path + '/public/index.html');
        });

 };

为什么我收到此错误????

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您可以尝试两种语法。 你传递的时候没有app.route。

index.js:

 'use strict';

 var path = process.cwd();

 module.exports = function (app){

    app.get('/',function (req, res) {
            res.sendFile(path + '/public/index.html');
        });

 };

或者这个

 'use strict';

 var path = process.cwd();
 var router = express.Router()
 module.exports = function (app){
   router.get('/', function (req, res) {
       res.sendFile(path + '/public/index.html');
   })

 };