0

This is my first time to use Node.js and I would like to run it on localhost. I had installed Node.js, but I don't know how to run the localhost.

can you guys help me? Below is my server.js code.

setting_detail = {};
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
if (process.env.NODE_ENV == 'production') {
    var cluster = require('cluster');
    if (cluster.isMaster) {
        // Count the machine's CPUs
        var cpuCount = require('os').cpus().length;

        // Create a worker for each CPU
        for (var i = 0; i < cpuCount; i += 1) {
            cluster.fork();
        }

        // Code to run if we're in a worker process
    } else {
        init();
    }
} else {
    init();
}

function init() {
    var port = 5000;
    var config = require('./config/config'),
        mongoose = require('./config/mongoose'),
        express = require('./config/express'),
        db = mongoose(),
        app = express();

    app.listen(port);
    var Settings = require('mongoose').model('Settings');
    Settings.findOne({}, function (error, setting) {
        setting_detail = setting
        console.log('Magic happens on port ' + port);
    });
    module.exports = app;
}
1
  • 2
    Did you try npm start? That's the general way. I recommend you read the documentation first and try something on your own before seeking help. :-) Commented Nov 30, 2018 at 7:22

2 Answers 2

8

You just need to run node server.js at your project path & then visit this -http://localhost:5000

Here is getting started guide for you.

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

Comments

-2

Simple Program for node

var express = require('express');
var app = express();

app.get('/', function (req, res) {
   res.send('Hello World');
})

var server = app.listen(8081, function () {
   var host = server.address().address
   var port = server.address().port

   console.log("Example app listening at http://%s:%s", host, port)
})

Source : https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm

for Mongoose: https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications

1 Comment

The OP already has a program written. They are asking how to run it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.