使用Upstart启动时,NodeJS应用程序不会加载静态文件

时间:2014-01-27 14:03:10

标签: node.js express upstart

我有一个NodeJS应用程序,我想从Upstart开始。到目前为止,该过程有效,Upstart启动应用程序,但是当我加载页面时,CSS没有加载。奇怪的是,如果我使用#node myapp.js启动应用程序,它将启动并正确加载文件。如果有的话,我不能解决这个问题。任何帮助,将不胜感激。

这就是我设置应用的方式:

var express = require('express'),
    config = require('getconfig'),
    path = require('path'),
    expressValidator = require('express-validator'),
    crypto = require('crypto'),
    app = express();

app.configure('development', function () {
    app.use(express.logger('dev'));
});

app.configure(function () {
    app.use(require('less-middleware')({ src: path.join(__dirname, 'public') }));
    app.use(express.static('public'));
    require('./bootstraps/handlebars')(app);
    app.use(express.favicon());
    app.use(express.limit('5mb'));
    app.use(express.urlencoded({limit: '5mb'}));
    app.use(express.multipart({limit: '5mb'}));
    app.use(express.json({limit: '5mb'}));
    app.use(express.cookieParser(config.http.cookieSecret));
    app.use(expressValidator());
    app.use(express.methodOverride());
    require('./bootstraps/session')(app);
    app.use(function(req,resp,next){
        resp.locals.session = req.session;
        next();
    });
    app.use(app.router);
    require('./bootstraps/controllers')(app);
});

app.listen(process.env.PORT || config.http.port);

module.exports = app;

...这是我的Upstart脚本:

#!upstart
description "My Project"
author      "Author"

start on startup
stop on shutdown

script
    export HOME="/root"

    echo $$ > /var/run/myprocess.pid
    su -s /bin/sh -c 'exec "$0" "$@"' theuser -- /root/local/bin/node /path/to/my_project/myapp.js >> /var/log/my_project/system.log 2>&1
end script

pre-start script
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/my_project/system.log
end script

pre-stop script
    rm /var/run/dreamcasino.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/my_project/system.log
end script

1 个答案:

答案 0 :(得分:3)

您可以使用chdir以使工作目录正确

script
    export HOME="/root"
    chdir /path/to/my_project/
    echo $$ > /var/run/myprocess.pid
    exec sudo -u theuser /root/local/bin/node /path/to/my_project/myapp.js >> /var/log/my_project/system.log 2>&1
end script