express.s上的router.get(' /')无效

时间:2015-02-27 03:18:19

标签: javascript jquery node.js express

我无法通过' /'访问我的快递应用程序的主页。路线模式。它正在处理/索引,例如我的快递版是4.6。

我尝试了app.use(' / *',路由器),但我的应用程序没有响应或503服务暂时不可用。它现在由' / index'模式和其他路线正常工作。只有' /'模式不起作用。 :)

这是我的代码段。



        var http = require('http');

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

        var router = express.Router();
        app.use('/', router);

        app.set('view engine', 'ejs');
        app.set('views', './views');
        app.use(express.static('./public'));

        var bodyParser = require("body-parser");
        app.use(bodyParser());

        var fs = require('fs');

        var clientSessions = require("client-sessions");

        var form = require('express-form');
        var field = form.field;

        var sha1 = require('sha1');

        var mysql = require('mysql');
        var connection = mysql.createConnection({
          host: process.env.OPENSHIFT_MYSQL_DB_HOST,
          port: process.env.OPENSHIFT_MYSQL_DB_PORT,
          user: process.env.OPENSHIFT_MYSQL_DB_USERNAME,
          password: process.env.OPENSHIFT_MYSQL_DB_PASSWORD,
          database: process.env.OPENSHIFT_GEAR_NAME
        });

        var multer = require('multer');
        var done = false;

        app.use(clientSessions({
          secret: 'xxxxx'
        }));

        app.use(function(req, res) {
          res.status(400);
          res.render('pages/404');
        });

         // Handle 500
        app.use(function(error, req, res, next) {
          res.status(500);
          res.render('pages/500');
        });

         //--- 
        app.use(multer({
          dest: 'public/uploads/',
          rename: function(fieldname, filename) {
            return filename + Date.now();
          },
          onFileUploadStart: function(file) {
            console.log(file.originalname + ' is starting ...')
          },
          onFileUploadComplete: function(file) {
            console.log(file.fieldname + ' uploaded to  ' + file.path)
            done = true;
          }
        }));

         //// --------------- start app routes --------------//
         // ----- GET -----
        router.get('/', function() {   // THIS PATTERN IS NOT WORKING
          console.log('hello world');  // this line is not working
          connection.query(strQuery, function(err, rows) {
            // res.render('pages/index');
          });

        });




3 个答案:

答案 0 :(得分:3)

您忘记第二行'*'

app.use('/*', router);

答案 1 :(得分:2)

因为您没有处理" /",这里是更新的代码

var router = express.Router();
app.use('/*', router);

router.get('/', function(req, res) {
    res.send('welcome home');
}
router.get('/index', function(req, res) {
    res.send('welcome index');
}

答案 2 :(得分:0)

你忘了重定向吗?

router.get("/", function (req, res) {
    res.redirect("/index");
});