Monday, 17 April 2017

I am not able to use multiple html files in node js

I am not able to use multiple html files in node js--
I am not able to use more HTML files I want to add 2-3 HTML links in my index.html so please tell me how can I use multiple HTML files in my code and see error shown below here i used 2 html pagelink and one css and one js pagelink. this is test.js file:

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

http.createServer(function (req, res) {

    if(req.url.indexOf('.html') != -1){ //req.url has the pathname, check if it conatins '.html'

      fs.readFile(__dirname + '/public/index.html', function (err, data) {
        if (err) console.log(err);
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data);
        res.end();
      });

    }

    if(req.url.indexOf('.js') != -1){ //req.url has the pathname, check if it conatins '.js'

      fs.readFile(__dirname + '/public/js/bootstrap.min.js', function (err, data) {
        if (err) console.log(err);
        res.writeHead(200, {'Content-Type': 'text/javascript'});
        res.write(data);
        res.end();
      });

    }


    if(req.url.indexOf('.css') != -1){ //req.url has the pathname, check if it conatins '.css'

      fs.readFile(__dirname + '/public/css/bootstrap.min.css', function (err, data) {
        if (err) console.log(err);
        res.writeHead(200, {'Content-Type': 'text/css'});
        res.write(data);
        res.end();
      });

    }
    if(req.url.indexOf('.html') != -1){ //req.url has the pathname, check if it conatins '.js'

      fs.readFile(__dirname + '/public/hello.html', function (err, data) {
        if (err) console.log(err);
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data);
        res.end();
      });

    }

}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');



i got this error in cmd while loading server
this is error in command line:

D:\Nodejs>node test.js
Server running at http://127.0.0.1:3000/
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at ServerResponse.OutgoingMessage.write (_http_outgoing.js:441:15)
    at D:\Nodejs\test.js:45:13
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:446:3)



via ashwini chavan

No comments:

Post a Comment