简体   繁体   English

使用Upstart启动时,NodeJS应用未加载静态文件

[英]NodeJS app not loading static files when started with Upstart

I have a NodeJS app that I want to start with Upstart. 我有一个想从Upstart开始的NodeJS应用程序。 So far the process works, Upstart initiates the app, but when I load the page, the CSS are not loading. 到目前为止,该过程仍然有效,Upstart会启动应用程序,但是当我加载页面时,CSS尚未加载。 The curious thing is that if I start the app using #node myapp.js, it will start and load the files correctly. 奇怪的是,如果我使用#node myapp.js启动应用程序,它将正确启动并加载文件。 I can't the difference, if any, that makes this issue possible. 如果有的话,我无法做到这一点。 ANy help would be appreciated. 任何帮助,将不胜感激。

This is how I've set up the app: 这是我设置应用程序的方式:

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;

... and this is my Upstart script: ...这是我的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

You can use chdir so that the working directory is correct 您可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM