ypbr
ypbr

Reputation: 216

How to run node js app with express on my server

I am new to Node.Js. I have to compile my sass files with gulp. I have created a gulpfile and node js app to run gulp task. I have an ASP.NET page. when I click a button I just need to create a sass file and run gulp task by calling my node app.

here is my node.js file

var express = require('express');
var app = express();
var exec = require('child_process').exec;

app.use(express.static('public'));
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.get('/send', function (req, res) {
    exec("gulp --gulpfile subdomain\\gulpfile.js");
    res.send('success!');
});

app.listen(5353, function () {
    console.log('Example app listening on port 5353!');
});

and this is my package.json file

"dependencies": {
    "child_process": "^1.0.2",
    "express": "^4.16.3",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.1",
    "gulp-cssmin": "^0.2.0",
    "gulp-filter": "^5.1.0",
    "gulp-sass": "^4.0.1"
  }

I am able to run node app but when I make an ajax call to my node app it always goes to my local pc not server.

function CreateCss(s, e) {    
    $.ajax({
        url: 'http://localhost:5353/send?domain=' + s.cpDomain,
        method: 'GET',
        success: function (res) {
            console.log(res);
        },
        error: function (response) {
            console.log('Error!');
            console.log(response);
        }
    });    
}

What should I do to call my node app on my server?

EDIT

update about localhost part.

When I change node js file as below

var options = {
    host: '35.34.35.34',
    port: 3001
};
app.listen(options, function () {
    console.log('Example app listening on port '+ options.port + '!');
});

it gives below error

Error: listen EADDRNOTAVAIL 35.34.35.34:3001
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at Server.setupListenHandle [as _listen2] (net.js:1338:19)
    at listenInCluster (net.js:1396:12)
    at doListen (net.js:1505:7)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:695:11)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

Upvotes: 0

Views: 114

Answers (1)

Saeed Jalali
Saeed Jalali

Reputation: 436

First add PORT in env variable and you can access it with process object node proccess object.

Like this:

var express = require('express');
var app = express();
var exec = require('child_process').exec;

const PORT = process.env.PORT || 3000;
app.use(express.static('public'));
app.use(function (req, res, next) {
   res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.get('/send', function (req, res) {
    exec("gulp --gulpfile subdomain\\gulpfile.js");
res.send('success!');
});

 app.listen(PORT, function () {
    console.log('Example app listening on port 5353!');
});

and set PORT in windows with this commnad :

set PORT=1234

Upvotes: 1

Related Questions