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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:40:04+00:00 2026-06-10T08:40:04+00:00

Im trying to learn node.js and have hit a bit of a roadblock. My

  • 0

Im trying to learn node.js and have hit a bit of a roadblock.

My issue is that i couldn’t seem to load an external css and js file into a html file.

GET http://localhost:8080/css/style.css 404 (Not Found) 
GET http://localhost:8080/js/script.css 404 (Not Found) 

(this was when all files were in the root of the app)

I was told to somewhat mimic the following app structure, add a route for the public dir to allow the webserver to serve the external files.

my app structure is like so

domain.com
  app/
    webserver.js

  public/
    chatclient.html

    js/
      script.js

    css/
      style.css

So my webserver.js script is in the root of app, and everything I want to access is in ‘public’.

I also saw this example that uses path.extname() to get any files extentions located in a path. (see the last code block).

So I’ve tried to combine the new site structure and this path.extname() example, to have the webserver allow access to any file in my public dir, so I can render the html file, which references the external js and css files.

My webserver.js looks like this.

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

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

  var myPath = url.parse(req.url).pathname;

    switch(myPath){

      case '/public':

        // get the extensions of the files inside this dir (.html, .js, .css)
        var extname = mypath.extname(path);

          switch (extname) {

            // get the html
            case '.html':
              fs.readFile(__dirname + '/public/chatclient.html', function (err, data) {
                if (err) return send404(res);
                res.writeHead(200, {'Content-Type': 'text/html'});
                res.write(data, 'utf8');
                res.end();
              });
            break;

            // get the script that /public/chatclient.html references
            case '.js':
              fs.readFile(__dirname + '/public/js/script.js', function (err, data) {
                if (err) return send404(res);
                res.writeHead(200, { 'Content-Type': 'text/javascript' });
                res.end(content, 'utf-8');
                res.end();
              });
            break;

            // get the styles that /public/chatclient.html references
            case '.css':
              fs.readFile(__dirname + '/public/css/style.css', function (err, data) {
                if (err) return send404(res);
                res.writeHead(200, { 'Content-Type': 'text/javascript' });
                res.end(content, 'utf-8');
                res.end();
              });
          }
          break;

          default: send404(res);
        }
    });

Inside the case of public, I’m trying to get any of the folders/files inside of this dir via
var extname = mypath.extname(path);
Similar to the link I provided.

But at the moment ‘extname’ is empty when I console log it.

Can anyone advise what I might need to add or tweek here?
I’m aware this can be done easily in Express, but I would like to know how to achieve the same thing just relying on Node.

I’s appreciate any help on this.

Thanks in advance.

  • 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-10T08:40:06+00:00Added an answer on June 10, 2026 at 8:40 am

    There are several problems with your code.

    1. Your server is not going to run as you have not specified a port to listen from.
    2. As Eric pointed out your case condition will fail as ‘public’ does not appear in the url.
    3. Your are referencing a non-existent variable ‘content’ in your js and css responses, should be ‘data’.
    4. You css content-type header should be text/css instead of text/javascript
    5. Specifying ‘utf8’ in the body is unnecessary.

    I have re-written your code.
    Notice I do not use case/switch. I prefer much simpler if and else, you can put them back if that’s your preference. The url and path modules are not necessary in my re-write, so I have removed them.

    var http = require('http'),
        fs = require('fs');
    
    http.createServer(function (req, res) {
    
        if(req.url.indexOf('.html') != -1){ //req.url has the pathname, check if it conatins '.html'
    
          fs.readFile(__dirname + '/public/chatclient.html', function (err, data) {
            if (err) console.log(err);
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(data);
            res.end();
          });
    
        }
    
        if(req.url.indexOf('.js') != -1){ //req.url has the pathname, check if it conatins '.js'
    
          fs.readFile(__dirname + '/public/js/script.js', function (err, data) {
            if (err) console.log(err);
            res.writeHead(200, {'Content-Type': 'text/javascript'});
            res.write(data);
            res.end();
          });
    
        }
    
        if(req.url.indexOf('.css') != -1){ //req.url has the pathname, check if it conatins '.css'
    
          fs.readFile(__dirname + '/public/css/style.css', function (err, data) {
            if (err) console.log(err);
            res.writeHead(200, {'Content-Type': 'text/css'});
            res.write(data);
            res.end();
          });
    
        }
    
    }).listen(1337, '127.0.0.1');
    console.log('Server running at http://127.0.0.1:1337/');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to learn a bit about c++ and have run in to some
I'm trying to learn Node and have the function: this.logMeIn = function(username,stream) { if
Currently I'm trying to learn what is node.js. I have installed node.exe in windows,
Hello my fellow ColdFusion developers! I'm trying to learn node.js, and I have a
I am trying to learn Node.js and some of points that I understand: Node.js
I'm new into Node.js and trying to learn. From what I have understood it's
Trying to learn Sencha Touch 2 and I can't seem to find out how
Trying to learn a bit about PDO and is going through this tutorial .
Trying to learn about php's arrays today. I have a set of arrays like
im trying to learn delegates and events in c#, i understand that an event

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.