Uncaught SyntaxError: Unexpected token Nodejs

1

I can not properly link my static / index files, follow the code

directory: 'blog / createServer.js'

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

var contentTypes = {
  'html': 'text/html',
  'css': 'text/css',
  'ico': 'image/x-icon',
  'png': 'image/png',
  'svg': 'image/svg+xml',
  'js': 'application/javascript',
  'otf': 'application/x-font-otf',
  'ttf': 'application/x-font-ttf',
  'eot': 'application/vnd.ms-fontobject',
  'woff': 'application/x-font-woff',
  'woff2': 'application/font-woff2',
  'zip': 'application/zip'
}

var server = http.createServer(function(request, response) {
  //A constante __dirname retorna o diretório raiz da aplicação.
  fs.readFile(path.join(__dirname, 'static', 'index.html'), function(err, html) {
    response.writeHeader(200, {
      'Context-Type': contentTypes['html', 'css', 'js']
    });
    response.write(html);
    response.end();
  })
})

server.listen(8000, function() {
  console.log("Executanto Site pessoal");
})

directory: 'blog / static / index.html'

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="libs/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>



    <script src="libs/jquery/dist/jquery.min.js"></script>
    <script src="libs/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="libs/angular/angular.min.js"></script>
    <script src="libs/angular-bootstrap/ui-bootstrap.min.js"></script>

</body>
</html>

Error

obs ... the files I want to link to are 'blog / static / libs /'

    
asked by anonymous 03.03.2017 / 03:30

1 answer

0

You are responding to requests for .css and .js files with content index.html , hence you are receiving < character errors within CSS and JS.

You need to read request.url to know what was requested and then act accordingly. An example would be as I wrote below. In a next step it would be better to have those files cached if you do not have NginX or another http server that does this.

Test the example below and confirm that you understand logic.

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

var contentTypes = {
    'html' : 'text/html',
    'css'  : 'text/css',
    'ico'  : 'image/x-icon',
    'png'  : 'image/png',
    'svg'  : 'image/svg+xml',
    'js'   : 'application/javascript',
    'otf'  : 'application/x-font-otf',
    'ttf'  : 'application/x-font-ttf',
    'eot'  : 'application/vnd.ms-fontobject',
    'woff' : 'application/x-font-woff',
    'woff2': 'application/font-woff2',
    'zip'  : 'application/zip'
};

var server = http.createServer(function(request,response){
    var ext = request.url.split('.').pop();
    if (contentTypes[ext]){
        var filename = request.url.split('/').pop();
        fs.readFile(path.join(__dirname, 'static', filename), function(err, str){
            response.writeHeader(200, {'Context-Type': contentTypes[ext]});
            response.write(str);
            response.end();
        });
    } else {
        // aqui deves servir os outros ficheiros
        // deixo aqui somente o 'index.html'
        fs.readFile(path.join(__dirname, 'index.html'), function(err, html){
            response.writeHeader(200, {'Context-Type': contentTypes.html});
            response.write(html);
            response.end();
        });
    }
});
server.listen(8000, function(){
    console.log("Executanto Site pessoal");
});
    
03.03.2017 / 10:06