在expressjs代码中获取无效的URL消息

时间:2016-11-22 11:53:30

标签: node.js express

我是express.js编码的新手。在下面的代码中,我想访问两个网址,例如http://localhost:3000http://localhost:3000/fetch,以使用get方法提供不同的请求。访问第一个URL时,我能够获得响应,但在访问第二个URL时,我收到404错误。我无法弄清楚这个问题,请你帮我解决一下。

以下是我的文件:

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 fetch = require('./routes/fetch');

var app = express();


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

app.use('/fetch',fetch);
app.use('/', index);

// 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: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

modules.export=app;

index.js

var express = require('express');

/* GET home page */
module.exports = (function() {
    var path = require('path');
    var bodyParser = require('body-parser');
    var db = require('./dbclient');
    db.dbconnection(null,null,null,null,'smartbin',null);
    var router = express.Router();
    router.get('/', function(req, res, next) {
        //res.render('index', { title: 'Express' });
        db.get('devicereg',{}).then(function(v){
            //for (i=0; i<v.length-1; i++)
                //res.json(v[i]);
            res.json(v);//.end();
        }).catch(function(v){
            console.log('[SmartBin:Error ' + v);
        });         
    });
    return router;
})();

fetch.js

var express = require('express');

/* GET home page. */
module.exports = (function(){
    var path = require('path');
    var bodyParser = require('body-parser');
    var router = express.Router();
    var db = require('./dbclient');
    db.dbconnection(null,null,null,null,'smartbin',null);
    router.get('/fetch', function(req, res, next) {
            db.get('devicereg',{}).then(function(v){
                res.json(v);
            }).catch(function(v)
                {console.log('[SmartBin:Error ' + v);}
            );
    });
    return router;
})();

1 个答案:

答案 0 :(得分:0)

我认为您可以尝试以下选项之一:

<强> 1。细微变化:

app.js中使用app.use('/', index)app.use('/', 'fetch')。您的路线设置在index.jsfetch.js文件中,因此它应该有效,即使没有app.js代码app.use('/fetch', 'fetch')

在现有代码中,您可以访问http://localhost:3000/fetch/fetch(因为您在/fetch中声明app.js,然后在/fetch文件中再次声明fetch.js

<强> 2。将app参数传递给路径文件:

app.js中,尝试要求您的路径文件传递app的实例,而不是app.use('/', index)app.use('/fetch', fetch)

E.g:

app.js

var express = require('express');
var app = express();

// Comment these lines
//app.use('/fetch',fetch);
//app.use('/', index);

// Add this lines passing the instance of 'app' you've created
var indexRoute = require('./routes/index')(app)
var indexFetch = require('./routes/fetch')(app)

在您的路线文件中,尝试按以下方式进行调整:

<强> index.js

module.exports = function(app) {
    app.get('/', function(req, res) {
        res.send("This is index");
    });
}

<强>取

module.exports = function(app) {
    app.get('/fetch', function(req, res) {
        res.send("This is fetch");
    });
}

希望它有所帮助。