Valentin Gjorgoski
Valentin Gjorgoski

Reputation: 305

How to start many node scripts at once, or connect each with main to start together

Maybe my question is not good but It is my first time using node.js I have index.js the main script that is the socket server. And I have another script for pull data from database. I starting main index.js with npm start on port 3000, and if I want to use the other script I need to start that script users_contacts.js on port 3001 or something else other than 3000. Is it another way to start and using both of the scripts.

users_contacts.js

var express   =    require("express");
var mysql = require('mysql');
var app       =    express();

var connection = mysql.createConnection({
    host: "192.168.0.0",
    port: "33991",
    user: "root",
    password: "******",
    database: "app_test"
});

connection.connect(function(err) {
    if (err) throw err;
    console.log("Connected!");
});

function handle_database(req,res) {
    // connection will be acquired automatically
    connection.query("select * from users",function(err,rows){
        if(err) {
            return res.json({'error': true, 'message': 'Error occurred'+err});
        }
        //connection will be released as well.
        res.json(rows);
    });
}

app.get("/",function(req,res){-
    handle_database(req,res);
});

app.listen(3002);

Upvotes: 0

Views: 122

Answers (1)

Robert garcia
Robert garcia

Reputation: 591

You cant run two processes in the same port, but you can concatenate your scripts this way:

script1 && script2 && script3...

Upvotes: 1

Related Questions