skip to Main Content

I didn’t get start server : 3001 message on console when I bush node app.js on terminal.

My app.js file content:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

const cors = require('cors');
var fs = require('fs');

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

var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server, {
  cors: {
    origin: "http://localhost:4200",
    methods: ["GET", "POST"]
  }
})

var user_socket_connect_list = [];

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

app.use(logger('dev'));
app.use(express.json({limit: '100'}));
app.use(express.urlencoded({ extended: true, limit: '100' }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

const corsOptions = {
  origin: "http://localhost:4200",
}

app.use(cors(corsOptions));

// import express inside dynamic add.
fs.readdirSync('./controllers').forEach((file) => {
  if(file.substr(-3) == ".js") {
    route = require('./controllers' + file);
    route.controller(app, io, user_socket_connect_list);
  }
})

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

server.listen(3001);

console.log("Server Start: 3001");

Array.prototype.swap = (x,y) => {
  var b = this[x];
  this[x] = this[y];
  this[y] = b;
  return this;
}

Array.prototype.insert = (index, item) => {
  this.splice(index,0, item);
}

Array.prototype.replace_null = (replace = '""') => {
  return JSON.parse(JSON.stringify(this).replace(/mull/g, replace));
}

String.prototype.replaceAll = (search, replacement) => {
  var target = this;
  return target.replace(new RegExp(search, 'g'), replacement);
}

default.json file content:

{
"dbConfig": {
    "host":"localhost",
    "user":"root",
    "password":"",
    "database":"online_groceries",
    "timezone":"utc+5:30",
    "charset":"utf8mb4"
}
}

login_controller and socket_controller js files content:

module.exports.controller = (app, io, socket_list) => {
    
}

I following this tutorial YouTube

How to solve that?

note: I don’t have experience with JavaScript programming language, I use it for iOS app.

2

Answers


  1. Try using a callback function, it will be more correct:

    server.listen(3001, (err) => {
         if(err) {
              console.error('HTTP Server listen error: ' + err);
              throw err;
         }
    
         console.log('Server started on port: 3001');
    })
    
    Login or Signup to reply.
  2. better to use framework like express.js

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search