Code Monkey home page Code Monkey logo

Comments (13)

antife-yinyue avatar antife-yinyue commented on July 22, 2024

我用的是Express,worker.js里有连接MongoDB的代码,这样是不是多次连接数据库了?

from pm.

aleafs avatar aleafs commented on July 22, 2024

代码弄上来给我看看

from pm.

antife-yinyue avatar antife-yinyue commented on July 22, 2024

就你wiki上的代码

from pm.

antife-yinyue avatar antife-yinyue commented on July 22, 2024

就在http.emit('connection', socket)下一行写了console

from pm.

antife-yinyue avatar antife-yinyue commented on July 22, 2024

具体代码得4号上班才能贴上来了

from pm.

antife-yinyue avatar antife-yinyue commented on July 22, 2024

另,如果一服务器有多个独立app,master.js要写几份?独立于各项目之外,还是跟项目走?

from pm.

aleafs avatar aleafs commented on July 22, 2024

肯定是随项目走了

from pm.

aleafs avatar aleafs commented on July 22, 2024

你console.log的意思是每个HTTP请求进来都打印一个ooxx

from pm.

antife-yinyue avatar antife-yinyue commented on July 22, 2024

master.js:

var master = require('pm').createMaster({
  pidfile: __dirname + '/tmp/jenny.pid'
})

master.register('http', __dirname + '/app.js', {
  listen: 3000
})

master.on('giveup', function(name, num, pause) {
  console.log('Master giveup to restart "%s" process after %d times. pm will try after %d ms.', name, fatals, pause)
})

master.dispatch()

app.js:

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

app.configure(function() {
  var db = mongoskin.db('xxxx', { safe: true })
  app.use(function(req, res, next) {
    req.db = db
    next()
  })
})

var http = require('http').createServer(app)
require('pm').createWorker().ready(function(socket, port) {
  http.emit('connection', socket)
  console.log(
    'Express server listening on port %d within %s environment.',
    port, app.get('env')
  )
})
$ node master.js

然后在浏览器访问localhost:3000

Snip20130104_1

from pm.

aleafs avatar aleafs commented on July 22, 2024

这个没错的,每个request打印一个console.log。
看你的意思应该是需要监听worker的listen事件,https://github.com/aleafs/pm/wiki/Worker

from pm.

antife-yinyue avatar antife-yinyue commented on July 22, 2024

app.js 调整下:

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

app.configure(function() {
  var db = mongoskin.db('xxxx', { safe: true })

  console.log('MongoDB connected')

  app.use(function(req, res, next) {
    req.db = db
    next()
  })
})

var http = require('http').createServer(app)
require('pm').createWorker().ready(function(socket, port) {
  http.emit('connection', socket)
})

console.log(
  'Express server listening on port %d within %s environment.',
  3000, app.get('env')
)
$ node master.js

Snip20130104_2

app.js被调用了 8 次?MongoDB也连了 8 次?

from pm.

aleafs avatar aleafs commented on July 22, 2024

你是8核CPU么?默认按CPU数起进程的

from pm.

antife-yinyue avatar antife-yinyue commented on July 22, 2024

4核的啊

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.