0

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
8
  • because you call localhost in ajax call Commented Aug 28, 2018 at 13:12
  • I realize that but I could not find how to change it and make it work? Commented Aug 28, 2018 at 13:13
  • you can deploy node.js app on server and add env variable for Port. after this call ajax with ip server and port Commented Aug 28, 2018 at 13:14
  • how can I do it? I tried some of examples that I have found on net but did not work. as I said I am new on node js. sorry. Commented Aug 28, 2018 at 13:16
  • you use windows server ? Commented Aug 28, 2018 at 13:17

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

still not working. is there any way to change host? I guess I should use www.abc.com for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.