Problems with localhost applications with AngularJS and NodeJS

2

I have some questions and problems trying to create a server with nodeJS .

The issue is that I have an application developed with very simple AngularJS and I want to make another one in nodeJS which basically loads AngularJS .

I created a little server using this code:

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


fs.readFile('./angular-js-web-form/index.html', function (err, html) {
    if (err) {
        throw err; 
    }       
    http.createServer(function(request, response) {  
        response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(html);  
        response.end();  
    }).listen(8000);
});

What it does is create a local server by reading the file index.html , which is the index of the application in angularJS :

Apparentlyitworks,butitcannotimportallstyles,allexternaljs,functions,andsoon.

Iknowthereareserversforthis,likegrunt,howeverIwantedtoavoidthem.Iwantmyangledapplicationtorunonlywithasimpleserverlikethisoneabove.

Doesanyonehaveanyideahowtohelp?Suggestion?

Thankyouinadvance.

ISSUE:

IendedupfindingthislinkwhichismoreorlesswhatIneed: link

Just that it still uses a module that is Express , it's only possible using it? Can not create local applications only by importing index and index doing import from the rest?

    
asked by anonymous 03.08.2015 / 21:17

1 answer

3

Your server code is working in part because you are only considering a content type ( "Content-Type": "text/html" ).

Try using the code below. Uses only NodeJS and some of the modules that come with NodeJS.

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

http.createServer(function (request, response) {
    console.log('request starting...');

    var filePath = '.' + request.url;
    if (filePath == './')
        filePath = './index.html';

    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
            break;
        case '.css':
            contentType = 'text/css';
            break;
        case '.json':
            contentType = 'application/json';
            break;
        case '.png':
           contentType = 'image/png';
           break;      
        case '.jpg':
           contentType = 'image/jpg';
           break;
        case '.wav':
           contentType = 'audio/wav';
           break;
}

fs.readFile(filePath, function(error, content) {
    if (error) {
        if(error.code == 'ENOENT'){
            fs.readFile('./404.html', function(error, content) {
                response.writeHead(200, { 'Content-Type': contentType });
                response.end(content, 'utf-8');
            });
        }
        else {
            response.writeHead(500);
            response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
            response.end(); 
        }
    }
    else {
        response.writeHead(200, { 'Content-Type': contentType });
        response.end(content, 'utf-8');
    }
});

}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
    
04.08.2015 / 16:56