I am trying to start json-server in the gulp task and I am checking if the server is running with the portInUse function. Like this:
var gulputil = require('gulp-util')
var run = require('gulp-run-command').default
var gulp = require('gulp')
const args = [
'json-server --watch .\\src\\main\\app\\reactjs\\api\\db.json --port 3005'
]
var net = require('net');
var portInUse = function(port, callback) {
var server = net.createServer(function(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(port, '127.0.0.1');
server.on('error', function (e) {
callback(true);
});
server.on('listening', function (e) {
server.close();
callback(false);
});
};
gulp.task("initLocalJsonServer", function() {
portInUse(3005, function(returnValue) {
gulputil.log('1 ' + returnValue);
});
run(args);
portInUse(3005, function(returnValue) {
gulputil.log('2 ' + returnValue);
});
});
This command, which is the args value here, works as intended when I run it on the command line, so there is nothing wrong with the command or the json server itself. If I run it, I will see json-server
inlocalhost:3005
Now the output from the portInUse function indicates that the server is starting, since the output:
[10:33:56] 1 false
[10:33:56] 2 true
But if I move on to localhost:3005
after completing gulp tasks, I donβt see any server running. What could be the reason?
source
share