Node.js产生多个进程

时间:2013-11-10 14:47:42

标签: javascript node.js process nodemon

我对node.js应用程序有点困惑。据我所知,node.js在一个进程中运行。但是,如果我通过调用node app.js来启动我的应用程序并使用htop监视它,我可以看到有4个子进程正在运行,我希望只有一个进程。

enter image description here

app.js

var express = require('express'),
    routes = require('./routes'),

    objects = require('./objects'),

    http = require('http'),
    path = require('path'),

    pinLayout = objects.pinlayout,

    // utils
    util = require('util'),
    wiringPi = require('wiring-pi'),
    winston = require('winston'),
    async = require('async');


// Logger - winston
var log = new(winston.Logger)({
    transports: [
        new(winston.transports.Console)({
            colorize: true,
            timestamp: true
        }),
        new(winston.transports.File)({
            filename: './log/app.log'
        })
    ]
});

// WiringPi
wiringPi.setup('gpio');

var app = express();

// all environments
app.set('port', process.env.PORT || 3001);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());

app.use(require('less-middleware')({
    src: __dirname + '/public',
    force: true,
    compress: true
}));

app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// 404 Page
app.use(function(req, res, next) {
    res.render('404.jade', {
        title: "404 - Page Not Found",
        showFullNav: false,
        status: 404,
        url: req.url
    });
});

// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
}

1 个答案:

答案 0 :(得分:4)

虽然您的代码在单个线程中运行,但node.js主要使用线程池进行文件系统操作。这是必需的,因为fs没有异步API。

例如,当您致电file.readFile时,您将通过Read(),会致电:

ASYNC_CALL(read, cb, fd, buf, len, pos);

read是阻止unix read(2)。这将在一个线程中运行,直到它完成。这些是你看到的主题。