我是Node.Js的新手。我必须用gulp编译我的sass文件。我已经创建了一个gulpfile和node js应用程序来运行gulp任务。我有一个ASP.NET页。当我单击一个按钮时,我只需要创建一个sass文件并通过调用节点应用程序来运行gulp任务。
这是我的node.js文件
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!');
});
这是我的package.json文件
"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"
}
我能够运行节点应用程序,但是当我对节点应用程序进行ajax调用时,它总是转到我的本地PC而不是服务器。
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);
}
});
}
我该怎么做才能在服务器上调用节点应用程序?
编辑
更新有关本地主机部分的信息。
当我如下更改节点js文件
var options = {
host: '35.34.35.34',
port: 3001
};
app.listen(options, function () {
console.log('Example app listening on port '+ options.port + '!');
});
它给出下面的错误
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
答案 0 :(得分:1)
首先将PORT添加到env变量中,然后可以使用process
对象node proccess object访问它。
像这样:
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!');
});
并使用以下命令在Windows中设置端口:
set PORT=1234