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 8296035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:49:20+00:00 2026-06-08T14:49:20+00:00

Is it possible to create a page (route) on the fly with node.js ?

  • 0

Is it possible to create a page (route) on the fly with node.js ?

Say I have a simple text input in a form on create-route.html file ( localhost:4001/create-route ), and I was to enter “my-page”,
This would then take me to a new view with the url of localhost:4001/my-page displaying a success message.

I’m not really sure if this is possible, because I’m also unsure what node modules would allow the client to access the createServer object, to manipulate it.
Looking around ‘Express’ is referenced a lot for routing , but I’m keen to see if this is possible in Node alone, before looking at a framework.

Any help is appreciated, thanks.

webserver.js

var http = require('http')
, url = require('url')
, fs = require('fs')
, server;

// declare http object
server = http.createServer(function(req,res){

  // parse the pathname as a url, get the trimmed route from the request
  var path = url.parse(req.url).pathname;

  // handle the various routes
  switch(path){

    case '/':
      fs.readFile(__dirname + '/index.html', function (err, data) {
        if (err) throw err;
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data, 'utf8');
        res.end();
      });


    case '/create-route':
        fs.readFile(__dirname + '/create-route.html', function (err, data) {
        if (err) throw err;
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data, 'utf8');
        res.end();
    });

    // example of dynamically created 'route'
    // "value' indicates the name that was inputted into the for on create-route.html, which in this case should be "my-page"

    case '/value':
        if (err) {throw err;}
        var body = "<html>" + 
            "<head>" +
            "<meta http-equiv='Content-Type' content='text/html'" +
            "charset=UTF-8 />" +
            "</head>" +
            "<body>" +
            "<p>youve just created a page called " + value + "</p>" +
            "</body>" +
            "</html>";
        res.writeHead(200, {"Content-Type": "text/html"});
        res.write(body);
        res.end();
    });

    break;

    default: send404(res);
  }
});

create-route.html

<html>
<head>
    <title>Create a route</title>
</head>
<body>
    <form action="/go-to-the-created-route">
        <label>Enter a route name:</label>
        <input type="text" name="create-route" id="create-route">
        <button type="submit">Submit</button>
    </form>
</body>
</html>

Updated Routes as per feedback

Here is the updated ‘go-to-created-route’ route as per @penartur feedback.

// Define the go-to-created-route
routes["go-to-created-route"] = function (req, res) {

// get the request url and split it to get the inputted value
var reqPath = req.url;
var splitPath = reqPath.split("=");
var routeName = splitPath[(splitPath.length - 1)]; // outputs the value entered into text input
//console.log(routeName); 

// create the new route by passing in the value of routeName, and display a page
routes[routeName] = function (req, res) {

// Not getting in here :(
    console.log("hello?");

    var body = "<html>" + 
        "<head>" +
        "<meta http-equiv='Content-Type' content='text/html'" +
        "charset=UTF-8 />" +
        "</head>" +
        "<body>" +
        "<p>youve just created a page called " + routeName + "</p>" +
        "</body>" +
        "</html>";
     res.writeHead(200, {"Content-Type": "text/html"});
     res.write(body);
     res.end();
};
};
  • 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-08T14:49:22+00:00Added an answer on June 8, 2026 at 2:49 pm

    It is a simple programming problem, not having much to do with the specific Node.js. The draft of the solution:

    var http = require('http')
    , url = require('url')
    , fs = require('fs')
    , server;
    
    var routes = {};
    
    routes["create-route"] = function (req, res) {
        fs.readFile(__dirname + '/create-route.html', function (err, data) {
            if (err) throw err;
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(data, 'utf8');
            res.end();
        });
    };
    
    routes["go-to-the-created-route"] = function (req, res) {
        var routeName = req.body["create-route"];
        routes[routeName] = function (req, res) {
            var body = "<html>" + 
                "<head>" +
                "<meta http-equiv='Content-Type' content='text/html'" +
                "charset=UTF-8 />" +
                "</head>" +
                "<body>" +
                "<p>youve just created a page called " + routeName + "</p>" +
                "</body>" +
                "</html>";
            res.writeHead(200, {"Content-Type": "text/html"});
            res.write(body);
            res.end();
        };
    };
    
    // declare http object
    server = http.createServer(function(req,res){
    
        // parse the pathname as a url, get the trimmed route from the request
        var path = url.parse(req.url).pathname;
    
        var firstPart = path.split('/')[1];
        if (routes.hasOwnProperty(firstPart)) {
            routes[firstPart](req, res);
        } else {
            send404(res);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How is it possible to create an simple Facebook landing page that just holds
I have looked on FaceBook Developer page and found that it's possible to create
Is it possible/easy to create a web page which would act like a Microsoft
Is it possible to create advance search option like that of google page in
Is it possible to create a Javascript and include it in a web page
Possible Duplicate: JavaScript library to create div-style window within page I would like to
I currently create an object on page load with all possible key/value pairs and
I want to create a Thread safe JSP page. It is possible in Servlet
Is it possible to create a page that redirects to a private page if
Is it possible to create a wiki page, where you mark a single piece

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.