2016-09-29 17 views
0

マイクロサービスのコレクションを作成しようとしています。問題は、私のルートが適切な場所にリダイレクトされていないようだということです。ここでExpressJSルーティングエラー

は私のindex.jsファイルです:

var express = require("express"); 
var app = express(); 
var port = process.env.PORT || 3000; 
var timestamp = require("./routes/timestamp"); 

app.listen(port, function(){ 
    console.log("Listening on port " + port); 
}); 

app.get("/", function(req, res){ 
    res.writeHead(200, {"Content-Type": "text/html"}); 
    res.end("Hi! This is a collection of microservices. <a href='https://github.com/Humad/timestamp-microservice'>See instructions here.</a>"); 
}); 

app.get("/timestamp", timestamp); 

そして、ここには私の 'タイムスタンプ' ファイルである:ここでは

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

router.get("/", function(req, res){ 
    res.send("Hello world!"); 
}); 

router.get("/:date", function(req, res){ 
    var newDate = req.params.date; 

    var natural = moment.utc(newDate, "MMMM D, YYYY", true); 
    var unix = moment.utc(newDate, "X", true); 

    if (natural.isValid() || unix.isValid()) { 
     if (natural.isValid()) { 
      newDate = natural; 
     } else { 
      newDate = unix; 
     } 

     res.json({unix: newDate.format("X"), natural: newDate.format("MMMM D, YYYY")}); 
    } else { 
     res.json({unix: null, natural: null}); 
    } 

    res.end();  
}); 

module.exports = router; 

起こることになっていただきました!されています

はlocalhost /タイムスタンプがなければなりません私に "Hello World!" はlocalhost /タイムスタンプ/ someDateは私に日付

とJSONファイルを与えるしかし、ここで実際に何が起こるかである必要があります。

はlocalhost /タイムスタンプが私に日付 はlocalhost /タイムスタンプ/ someDateとJSONファイルを与える私に与えますエラーを取得できません "Hello World"は一度も表示されません

+0

dateパラメータを指定していないので、localhost/timestampはエラーを送出しませんか? – kalin

答えて

3

app.use("/timestamp", timestamp);に変更してください。

タイムスタンプルータミドルウェア/timestampパスにマウントしようとしていると思いますが、現在行っているGETリクエストへのコールバックではありません。