How to publish applications done in Node.js

Asked

Viewed 3,827 times

14

Well, I’m new with Node.js, I was able to build an example application, I learned a lot from Node with Express, but I can’t understand how to publish the application on the linux server. Does anyone know how to publish? I would have to create a process for it to automatically run the command node app.js ?

Note: The Server has the Node installed.

3 answers

8


3

you will need to create a service start script using the packages cited by @Cigano

#!/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

0

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

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

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

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

Below an example of a package.json simple that will serve p/ 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 working locally and not on your server, it is likely that the port you are reporting is not the one specified by the server. For example I use as a server the Appfog, and the port to be used by the application is informed via an environment variable process.env.VCAP_APP_PORT.

In my application it is more or less like this:

app.set("port", process.env.VCAP_APP_PORT || 3000);
...
http.createServer(app).listen(app.get("port"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.