Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
How serve client javascript modules in node.js

I'm new programmer to node.js. I trying to create vanilla server in node.js. In my client, I used ES6 modules. when I start my server and search for http://localhost:3000/ in my browser, HTML and CSS loaded but for javascript have this error:

enter image description here

I have four javascript modules for client side and in HTML I use this code for load javascript moduls:

<script type="module" src="js/controller.js" async></script>

My server code :

const http = require('http');
const fs = require('fs');
const path = require('path');

const PORT = 3000;
const server = http.createServer();

server.on('request', (req, res) => {
    let routes = {
        'GET': {
            '/': indexHtml,
        }
    }
    console.log(req.url)
    let handler = routes[req.method][req.url];

    handler = handler || readFile;
    handler(req, res);
})

server.listen(PORT, () => {
    console.log(`Listening on port ${PORT}...`)
});

function indexHtml(req, res) {
    readFile({ url: '/index.html' }, res);
}

function readFile(req, res) {
    var filePath = path.join(__dirname, '../client/', req.url);
    // console.log(filePath)
    fs.readFile(filePath, (error, data) => {
        if (error) {
            res.writeHead(404);
            res.write('Not found error 404');
            res.end()
        } else {
            res.writeHead(200);
            res.write(data);
            res.end();
        }
    })
}

How can I solve this error and serve javascript modules, Thanks a lot for your help.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

With comment @derpirscher, I change my reader function with this code :

function readFile(req, res) {
    var filePath = path.join(__dirname, '../client/', req.url);

    fs.readFile(filePath, (error, data) => {
        if (error) {
            res.setHeader('Content-Type', 'text/html');
            res.writeHead(404);
            res.write('Not found error 404');
            res.end()
        } else {
            const url = req.url === '/' ? '/index.html' : req.url;
            if (req.url.includes('js')) res.setHeader('Content-Type', 'application/javascript');
            if (req.url.includes('css')) res.setHeader('Content-Type', 'text/css');
            if (req.url.includes('html')) res.setHeader('Content-Type', 'text/html');
            res.writeHead(200);
            res.write(data);
            res.end();
        }
    })
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!