How to publish application made in Node.js

13

Well, I'm new to Node.js, I was able to set up an example application, learned a lot of Node with Express, but I can not figure out how to publish the application to the linux server. Does anyone know how to publish? I would have to create a process for it to automatically run the node app.js command?

Note: The server has the node installed.

    
asked by anonymous 12.02.2014 / 20:03

3 answers

7

There are a few ways. The ones I know are:

  • Using the package forever : link
  • Using supervisord + nodemon

Just send the files to the server, configure Express to serve on an accessible port and run its server.js using one of the packages above.

If you need to redirect the port, use nginx : link

    
12.02.2014 / 20:16
3

You will need to create a service start script using the packages quoted by @Cyano

#!/bin/bash
JSHOME=/opt/homeapp #home de suas apps..
DIR=$JSHOME/js/ #diretorio dos apps
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin #caso tenha o node fora do path padrão
NODE_PATH=$JSHOME/node_modules #path com pacotes para seu app questão de isolamento
# no final ficaria algo como
# app em /opt/homeapp/js/appfile.js
# nodepath como /opt/homeapp/js/node_modules
NODE=node
NODE_ENV=production
APP="appfile"
test -x 'which $NODE' || exit 0
function start_app {
        ###note que NODE_ENV e NODE_PATH são variaveis definidas para o ambiente de execução do nohup
        # o | cronolog é opcional... fiz para registrar logs por data.. cronolog é velho não use se não precisar
        NODE_PATH=$NODE_PATH NODE_ENV=$NODE_ENV nohup $NODE $DIR/${APP}.js | /usr/sbin/cronolog $DIR/log/%Y/%m/%d/${APP}.log 2>&1 &
        echo $! > "$DIR/${APP}.pid"
}
function stop_app {
        killall node > /dev/null 2>&1
        echo ok
}
case $1 in
        start)
                start_app 
        ;;
        stop)
                stop_app
        ;;
        restart)
                stop_app
                start_app
        ;;
        *)
                echo "usage: render-image {start|stop}" ;;
esac
exit 0
    
12.02.2014 / 20:41
0

You just need to add this code snippet to your package.json :

"scripts": {
    "start": "node app.js"
}

You can add numerous commands that run at various points in the life cycle of your package. For more details you can take a look at this link where it has the full specification.

By default the server expects to find a server.js file in the project root, as in your case the file you need to execute is the app.js you need to specify it.

Below is an example of a simple% wizard that will work for you.

{
    "name": "[NOME DA SUA APLICAÇÃO]",
    "version": "[VERSÃO]",
    "private": true,
    "scripts": {
        "start": "node app.js"
    },
    "dependencies": {
        "express": "~3.4.8"
    }
}

If your application is still running locally and your server is not, it is very likely that the port you are reporting is not the one specified by the server. For example I use AppFog as the server, and the port to be used by the application is informed by an environment variable package.json .

In my application it looks something like this:

app.set("port", process.env.VCAP_APP_PORT || 3000);
...
http.createServer(app).listen(app.get("port"));
    
12.02.2014 / 21:58