0

I want to create a function that return a http.Server and Serve the text of the file testText.txt in the body of the HTTP response when a GET request is made to the '/' route. Parse the request for a "paragraphs" parameter. That parameter must be an integer and represent the number of paragraph you want to receive starting from the beginning of the test text. Return the appropriate content in the body of the response. If any error happens, return a 500 error. If the provided route is not known, return a 404 error.

here is what i have so far

function makeServer() {
    return http.createServer(function(req, res){
    if(req.url === '/'){  
        fs.readFile('testText.txt', function(err , para){
            console.log("Data", para);
            res.end();
        });
    console.log("The end");
    }
}
9
  • 1
    Can you please add the Error which you are getting from above code? Commented Jan 21, 2020 at 5:18
  • How are you passing "paragraphs" in your request, I am not able to understand correctly Commented Jan 21, 2020 at 5:33
  • the testText.txt contains the paragraph Commented Jan 21, 2020 at 5:38
  • @Sohan first we readt the testText.txt and then Parse the request for a "paragraphs" parameter Commented Jan 21, 2020 at 5:49
  • @Jhon why you are not using express JS? Commented Jan 21, 2020 at 5:56

3 Answers 3

1

I would expect to do something like this,

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

//Handle 404 here
app.use(function (req, res, next) {
  res.status(404).send({
               message: "Page Not Found"
           }) 
  });

Inject the GET request to your default route

app.get('/', (req, res) => {          
    // **modify your existing code here**
    fs.readFile('testText.txt', (e, para) => {
        if (e) {
         res.status(500).send({
               message: "Something went wrong"
           }) 
         }
        res.send(para);
    });
});

app.listen(5555);
Sign up to request clarification or add additional context in comments.

4 Comments

He's not using express.! For your note, use status code 404 instead of 400.
Update error code, I just asked in above comments why he is not using express JS. He is ok to use if shown with proper example
how do we handle the " That parameter must be an integer and represent the number of paragraph you want to receive starting from the beginning of the test text." ? Sir
That is your logic
0

As you have mentioned in your question use that err object inside the function such as below:

function makeServer() {
    return http.createServer(function(req, res){
    if(req.url === '/'){  
        fs.readFile('testText.txt', function(err , para){
            if (err) {
               res.status(500).send({
                   message: "Something went wrong"
               })
               // error handling 
            } else  {
            console.log("Data", para);
            res.end();
            }
        });
    console.log("The end");
    }
}

6 Comments

Where are we parsing paragraph param here?
@JhonCull please check updated answer, I've modified it with 500 error, you can use response object to share the status code and the message as per the error.
404 error for route ? like if someone tries to call the route with something like /abc or /error then you have to create a new route request and as mentioned in answer use response object with .status(404) and .send() for message
@Sohan the testText.txt contains list of text paragraphs Sir
can you provide a 404 example SIr ?
|
-1

Firstly, Welcome to the node world...

1) Work with file in res

Please refer this answer. It will help you.

2) Error code 500 if any error

res.status(500).json({success: 0, error 'Something went wrong'});

3) For handle 404 if route not matched

var createError = require('http-errors'); 

//Install via this command-> npm i http-errors --save

app.use(function (req, res, next) {
  next(createError(404));
});

3 Comments

So i will be using express Sir ?, can you provide the full aswer to my questio Sir ? , Thank you.
But I am using and online browser compiler Sir
@JhonCull The best practice is to work with real node API with postman. And other developers had shared an answers so I don't think so I need to share an answer. You may use it. Let me know if you have any other query. :)

Your Answer

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