Uncaught Syntaxerror: Unexpected token Nodejs

Asked

Viewed 1,686 times

2

I cannot 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

inserir a descrição da imagem aqui

Obs... the files I want to link are in 'blog/Static/libs/'

1 answer

1


You’re responding to file requests .css and .js with the content of index.html, so you’re getting character errors < within CSS and JS.

You have to read the request.url to know what was requested and then act upon it. An example would be how I wrote down. In a next step it would be better to have these files cached, if you don’t 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");
});

Browser other questions tagged

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