GithubHelp home page GithubHelp logo

express 2.x app.error 无法注册 about pm HOT 4 CLOSED

 avatar commented on May 17, 2024
express 2.x app.error 无法注册

from pm.

Comments (4)

fengmk2 avatar fengmk2 commented on May 17, 2024

@yuest 能给一段最简单的示例代码吗?我贴到readme里面去。

from pm.

 avatar commented on May 17, 2024
var app = require('express').createServer()
app.get('/error', function(req, res, next) {
  next(new Error('error'))
})
app.error(function(err, req, res) {
  res.statusCode = 500
  res.end(err.message)
})

//app.listen(8080) //没问题

//使用 node-cluster 有问题
//因为 app 没有触发 listening
//可以在此加一句
//app.emit('listening')

var worker = require('node-cluster').Worker()
worker.ready(function(socket) {
  app.emit('connection', socket)
})

不过推荐不使用 app.error,而用 app.use(function(err, req, res, next) {}) 四个参数的 middleware
express 3.0 会去掉 app.error

from pm.

 avatar commented on May 17, 2024

又想了一下,建议是不是加一个 worker.listen()

在 worker 里面 (new require('node-cluster').Worker).listen(app) 这样,可以简化为 cluster.listen(app)

因为现在的调用方式直接传递 socket,封装一下让 node-cluster 库来做这个事情,将来 node 有升级的话也能从 node-cluster 库这边来跟进。

from pm.

aleafs avatar aleafs commented on May 17, 2024

之前考虑过这种方式,每个worker遵从固定的interface去实现逻辑,与socket的交互完全由node-cluster去做。但这种方法要做到通用化的解决方案势必把这个interface搞得很复杂,还没完全想清楚怎么做。yuest有思路可以提一个pull过来吗?

from pm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.