Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9121693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:55:12+00:00 2026-06-17T05:55:12+00:00

I am new to node js. I was trying to create a simple HTTP

  • 0

I am new to node js. I was trying to create a simple HTTP server. I followed the famous example and created a ‘Hello World!’ server as follows.

var handleRequest = function(req, res) {
  res.writeHead(200);
  res1.end('Hello, World!\n');
};

require('http').createServer(handleRequest).listen(8080);

console.log('Server started on port 8080');

Running this code would start the server properly as expected. But trying to access http://127.0.0.1:8080 would crash it by throwing an error that res1 is not defined. I would like to have the server still continue running and gracefully report errors whenever it encounters it.

How do I achieve it? I tried try-catch but that isn’t helping me 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:55:13+00:00Added an answer on June 17, 2026 at 5:55 am

    There are a bunch of comments here. First of all, for your example server to work, handleRequest needs to be defined BEFORE using it.

    1- What you actually want, which is preventing the process to exit, can be handled by handling uncaughtException (documentation) event:

    var handleRequest = function(req, res) {
        res.writeHead(200);
        res1.end('Hello, World!\n');
    };
    var server = require('http').createServer(handleRequest);
    process.on('uncaughtException', function(ex) {
        // do something with exception
    });
    server.listen(8080);
    console.log('Server started on port 8080');
    

    2- I would recomment to use try{} catch(e) {} on your code, such as:

    var handleRequest = function(req, res) {
        try {
          res.writeHead(200);
          res1.end('Hello, World!\n');
        } catch(e) {
          res.writeHead(200);
          res.end('Boo');
        }
    };
    

    3- I guess the example was just an example and not actual code, this is a parsing error that can be prevented. I mention this, since you NEED to NOT have parsing errors on Exception catch handlers.

    4- Please note that node process is going to be replaced in the future with domain

    5- I’d rather use a framework like express, than doing this stuff.

    6- Recommended lecture: StackOverflow – NodeJS best practice for exception handling

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a new node type like this: http://www.live-wtr.ru/leo/Tikz9.png but don't
I'm trying to make a simple custom field field_book_year for the new node type
I am trying to create a new node that is a child of an
I'm trying to create a simple chat application with node.js and jQuery but I'm
I'm trying to create sample server plugin for Neo4j REST server based on http://docs.neo4j.org/chunked/snapshot/server-plugins.html
Hello I am trying to create a simple neo4j DB. I have a for
New to Node.js and Express, I am trying to understand the two seems overlapping
I'm trying to add a new nova-compute node (KUubuntu 12.04) to my single node
I am new to hadoop and trying to get a single node setup of
I want to create new node of BeanTreeView, and when I add some node

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.