chirag lathiya chirag lathiya - 25 days ago 13
Node.js Question

Node.js listen EADDRNOTAVAIL for shared pc call using ipaddress

i am use node.js for restAPI & make an application but for test at localhost i want to use an another shared PC so i add some code as suggested here and also add code like

app.listen(3000, '192.168.100.102', function() {
console.log('Listening to port: ' + 3000);
});


but i got an error like thisenter image description here

i am use default express js structure using defined here

if you want to show my code then it's like

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');



var routes = require('./routes/index');
var users = require('./routes/users');



var funclib = require('./routes/functions/functionLib');

var moduleNm = 'app.js';
var dbfunction = require('./routes/functions/dbfunction');
var connectDB = dbfunction.connectDB(moduleNm);

var nodeSmtp = require('./routes/functions/nodemailer_smtp');

var mysql = require('mysql');
var connection = require("express-myconnection");

var app = express();

app.set('host', '192.168.100.102');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/', routes);
app.use('/users', users);



// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


app.listen(3000, '192.168.100.102', function() {
console.log('Listening to port: ' + 3000);
});

module.exports = app;


i hope details are enough.
thanks in advance

Answer

You have to either use a valid IP address that is assigned to a network interface on that system (or '0.0.0.0'/'::1' to listen on all interfaces).