app.set然后TypeError:app.get不是一个函数

时间:2016-09-17 13:55:28

标签: javascript node.js express

使用express 4.x时,我在我的server.js中设置端口,如下所示。

var express = require('express');
var app = express();
...
var port = process.env.PORT || 8080;
app.set('port', port);
...
module.exports = app;

但是当我尝试在我的路线文件中访问它时,如下所示......

// path to routes file is app/models, hence the '../../'
var app = require('../../server');

// default route
router.get('/', function (req, res) {
    res.send('Hello! The API is at http://localhost:' + app.get('port') + '/api');
});

...我收到以下错误。

TypeError: app.get is not a function

到底是怎么回事?

4 个答案:

答案 0 :(得分:3)

好的,我终于明白了。 app未在路由文件中正确设置,因为我们之前在server.js中module.exports = app之后执行了require('./app/models/routes');。所以,一旦我将应用程序的导出移动到需要路由文件之前......一切正常!

答案 1 :(得分:0)

我不确切知道发生了什么,我不知道你是否想要这个解决方案。我曾遇到类似的问题。

在主文件中我做了类似

的操作
<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <button type="button" id="stop">Stop</button>
    <button type="button" id="left">Left</button>
    <button type="button" id="right">Right</button>
    <canvas id="canvas" width="600" height="400"></canvas>

    <script>
        window.requestAnimFrame = (function () {
            return window.requestAnimationFrame || 
                   window.webkitRequestAnimationFrame || 
                   window.mozRequestAnimationFrame || 
                   window.oRequestAnimationFrame || 
                   window.msRequestAnimationFrame || 
                   function ( /* function */ callback, /* DOMElement */ element) {
                       window.setTimeout(callback, 1000 / 60);
                   };
        })();

        var canvas = document.getElementById("canvas"), ctx = canvas.getContext("2d");
        canvas.width = 400;
        canvas.height = 200;

        var points = [],
            currentPoint = 1,
            nextTime = new Date().getTime() + 500,
            pace = 150;

        // make some points
        for (var i = 0; i < 50; i++) {
            points.push({
                x: i * (canvas.width / 50),
                y: 100
            });
        }

        function draw(runAnimation) {
            if (runAnimation.value) {
                if (new Date().getTime() > nextTime) {
                    nextTime = new Date().getTime() + pace;

                    currentPoint++;
                    if (currentPoint > points.length) {
                        currentPoint = 0;
                    }
                }
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.beginPath();
                ctx.moveTo(points[0].x, points[0].y);
                ctx.lineWidth = 2;
                ctx.strokeStyle = '#2068A8';
                ctx.fillStyle = '#2068A8';
                for (var p = 1, plen = currentPoint; p < plen; p++) {
                    ctx.lineTo(points[p].x, points[p].y);
                }
                ctx.stroke();

                requestAnimFrame(draw(runAnimation));

            }
        }

        var stop = document.getElementById('stop');
        var left = document.getElementById('left');
        var right = document.getElementById('right');
        /*
         * define the runAnimation boolean as an obect
         * so that it can be modified by reference
         */
        var runAnimation = {
            value: false
        };

        stop.addEventListener('click', function () {
            runAnimation.value = !runAnimation.value;

            if (runAnimation.value) {
                requestAnimationFrame(draw(runAnimation));
            }
        });

        left.addEventListener('click', function () {

        });

        right.addEventListener('click', function () {

        });
    </script>
  </body>
</html>      

在路线中我做了类似

的事情

你所拥有的&#34; ./ routes / path&#34;文件:

var express = require('express');
var app = express();
....
var routes = require("./routes/path")
routes(app);

你看我通过了快递应用程序。

基本上,routes是一个将app作为参数的函数。

我尝试了app.set(&#34; app&#34;,app),我认为这不起作用。

答案 2 :(得分:0)

看起来app没有被定义。你也可以尝试这样的事情。

router.get('/', function (req, res) {
    var app =req.app.get("app")
    res.send('Hello! The API is at http://localhost:' + app.get("port") + '/api');
});

https://stackoverflow.com/a/15018006/1893672

答案 3 :(得分:0)

在处理GET,POST等路线时,您应该收到“ req”依赖项。应用应附加到此,因此只需引用req.app:

router.get('/',function(req,res,next){
    console.log(app.get('secret'));
}

不需要require语句。