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

382
Views
JavaScript code that includes Node.js does not execute completely

The following is the code saved in an index.js file when i run my javascript code (that includes node js) it does not execute it completely.

var fs = require('fs');
var http = require('http');
var url = require('url');
var glob = require( 'glob' );
var language_dict = {};

glob.sync( './language/*.json' ).forEach( function( file ) {
    let dash = file.split("/");
    
    if(dash.length == 3) {
        let dot = dash[2].split(".");
        
        if(dot.length == 2) {
            let lang = dot[0];
            
            fs.readFile(file, function(err, data) {
                language_dict[lang] = JSON.parse(data.toString());
            });
        }
    }
});

http.createServer(function (req, res) {
    var q = url.parse(req.url, true);
    var lang = 'en';
    let dash = q.pathname.split("/");
    
    if(dash.length >= 2) {
        let code = dash[1];
        
        if(code !== '' && language_dict.hasOwnProperty(code)) {
            lang = code;
        }
    }
    
    fs.readFile('index.html', function(err, data) {
        res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
        let data_string = data.toString();
        
        for (var key of Object.keys(language_dict[lang])) {
            let pattern = new RegExp("{{" + key + "}}", "g");
            data_string = data_string.replace(pattern, language_dict[lang][key]);
        }
        
        res.write(data_string);
        return res.end();
    });
}).listen(8080);

The following is what happens in the output when i use: node "path": enter image description here

This is what happens when i used: node index.js: enter image description here

about 4 years ago · Juan Pablo Isaza
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!