Problems with localhost application with Angularjs and Nodejs

Asked

Viewed 1,168 times

2

I’m having some questions and problems trying to create a server with nodejs.

The point is I have an application developed with Angularjs very simple and want to do another in nodejs which basically carries the 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, this which is the index of the application in Angularjs:

app angular aberto

Apparently it works, but it can’t import all the Tyles, all the js external, functions and etc.

I know there are servers for that, like Grunt, but wanted to avoid them. I want my angular application to run only with a simple server like this one above.

Does anyone have any idea how to help? Suggestion?

Already I leave my thanks.

EDITION:

I ended up finding this link that is more or less what I need: https://thinkster.io/mean-stack-tutorial/

only that it still uses a module that is the Express, only possible using it? There is no way to create local applications just by importing the index and the index doing the import of the rest?

1 answer

3


Your server code is working in part because this considering only one type of content ("Content-Type": "text/html").

Try using the code below. Use only Nodejs and some of the module that comes 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/');
  • Friend, thank you for your attention, however, the result is still the same

  • Weird. I’m running this code here on the local machine and the server works perfectly: loads JS, CSS, Images and etc. @Paulogustavo

  • What I did was copy this code and put it into a JS file and run it with the Node readHtml.js command. Was it the structure of my project that was wrong? That nodejs file is in the /home/paulo folder. s/test and the application is inside /home/paulo. s/test/app. I changed the code there to get the . /app/index.html.

  • @Paulogustavo Following the structure that Oce spoke above, after you run the command `Node readHtml.js' inside the folder " /home/paulo. s/test" Voce must access the url: "http://127.0.0.1:8000/app". Thus the file "app/index.html" must load its application.

  • Friend, really, the mistake was my own. With that last comment that touched me fafauh. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.