0

I am trying to test a simple javascript file but unable to test as on my browser the page loads for forever without any warnings and at the bottom side a text bar appears saying "waiting for localhost..." On bash, I'm typing node app.js and after hitting enter terminal says "Server Has Started" as expected but when I go to "http://localhost:3000" the page keeps on loading forever. I've Installed node and express rightly. (I was not able to include express here as I don't know how to.) Please be nice, I am new to the world of development.

// Express Setup

let express = require("express");
let app = express();


// Setting Up Routes

app.get("/",function(req,res) {  
    res.send = "Hi There, Welcome To My Assignement!";
});


app.get("/:actionName/pig", function(req , res){
    res.send = "The Pig Says \"Oink\" !";
});

app.get("/:actionName/dog", function(req , res){
    res.send = "The Dog Says \"Woof\" !";
});

app.get("/:actionName/cow", function(req , res){
    res.send = "The cow Says \"Moo\" !";
});


app.get("/repeat/:string/:times",function(req,res){
    let times = parseInt(app.param.times);
    for (let i = 0 ; i <= times ; i++) {
        res.send = app.param.toString;
    }
});

app.get("*" , function(req,res){
    res.send = "Error 404, Page not found!";
});


// Setting Port Up

app.listen(3000, function () {
  console.log("Server Has Started!");
});

2
  • I think the issue is with the way you are trying to send back the response, correct me if im wrong, but I think what you want to do is something like: res.send("content") otherwise you are overwriting the 'send' function, and express will not return any result. Commented Jun 22, 2020 at 13:06
  • PD: Because the page was loading, we could assume the HTTP server was working. Otherwise, you would've seen an ERR_CONNECTION_REFUSED message Commented Jun 22, 2020 at 13:08

2 Answers 2

4

You should not override res.send, it is a function that you should call with values that you want to send to user.

For example your root route should look like this:

app.get("/", function(req,res) {  
    res.send("Hi There, Welcome To My Assignement!");
});
Sign up to request clarification or add additional context in comments.

Comments

0

In order to be run on browser, You will need to tell your app to listen to some specific port:

I added below snippet to the code you provided:

app.listen(port, () => {
  console.log(`Listening to requests on http://localhost:${port}`);
});

Also res.send should not be assigned, instead passed the reponse to be shown

Check this REPL:

Express App

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.