节点快速路由无法解析

时间:2014-10-14 21:00:39

标签: node.js express routing coffeescript

我试图捕获所有请求,从" / apiv1"开始,但由于某种原因路由无法解析。

这是我的app.coffee(底部定义的路线):

require('better-require')()
express = require("express")
router = express.Router()
http = require("http")
https = require('https')
path = require('path')
request = require("request")
fs = require('fs')
morgan = require('morgan')
bodyParser = require('body-parser')
cookieParser = require('cookie-parser')
serveStatic = require('serve-static')
csrf = require('csurf')
bcrypt = require('bcrypt')
querystring = require('querystring')
winston = require('winston')

# server config
server =
  http:
    port: process.env.PORT or config.server.http.port
  https:
    port: process.env.PORTSSL or config.server.https.port
    options:
      ca: ca
      key: fs.readFileSync(config.ssl.certificates.key)
      cert: fs.readFileSync(config.ssl.certificates.cert)

app = express()

app.use serveStatic(__dirname + "/_public")

# set up the logger
app.use morgan("combined")

# parse application/x-www-form-urlencoded
app.use bodyParser.urlencoded(extended: false)

# parse application/json
app.use bodyParser.json()

app.use cookieParser()
app.use (req, res) ->
  res.sendFile __dirname + assetUrl + "/index.html"

https.createServer(server.https.options, app).listen server.https.port
http.createServer(app).listen server.http.port

# set up the routes
apiRoutes = require('./routes/api')
app.use('/apiv1', apiRoutes)

我的api.coffee路线文件:

express = require("express")
router = express.Router()

router.get "*", (req, res) ->
  console.log "api route!"

module.exports = router

有关为什么路线没有得到解决的任何想法?

1 个答案:

答案 0 :(得分:0)

我的猜测是,您在所有请求中看到index.html,因为您的路由之前有一个中间件响应每个请求res.sendFile __dirname + assetUrl + "/index.html"