javascript - add css file with file system - fs - in nodejs -


i'm trying use nodejs make website. added fs module , use external file present index.html. that's ok. have css file style index.html, named index.css , not work.

my code:

<!doctype html>  <html>    <head>  	<meta charset="utf-8"/>  	<title>some title</title>  	<link rel="stylesheet" type="text/css" href="test/index.css">  </head>    <body lang="pt-br">  	<img src="_image/logonp.png" id="imglogologin">  	<div id="divlogin">    		<form name="login">    			<input type="text" id="idnome" name="nnome" size="20" maxlength="30" placeholder="nome" autofocus onkeydown = "if(event.keycode == 13)document.getelementbyid('idsenha').focus()"><br>    			<input type="password" id="idsenha" name="nsenha" size="20" maxlength="20" placeholder="senha" onkeydown = "if(event.keycode == 13)document.getelementbyid('idbutton').click()">    			<input type="button" id="idbutton" onclick="verifylogin(this.form)" value="login" >    		</form>    	</div>  </body>    </html>

files:

  • index.html (projectfolder/test/index.html)
  • test.js (projectfolder/test.js)
  • index.css (projectfolder/test/index.css)

my test.js file:

var http = require('http'); var fs = require('fs');  http.createserver(function(request, response){   if(request.url == '/'){     fs.readfile('test/index.html', function(err, html){       response.writehead(200, {'content-type': 'text/html'});       if(err) response.write('index nao encontrado');       response.write(html);       response.end();     });   }   else if(request.url == '/second'){     response.writehead(200, {'content-type': 'text/html'});     response.write("<h1> second page </h1>");     response.end();   }   else {     response.writehead(200, {'content-type': 'text/html'});     response.write("<h1> incorrect page </h1>");       response.end();   } }).listen(3000, function(){   console.log('server started'); }); 

you should add bunch of code below response css file:

if(request.url == '/test'){ fs.readfile('test/index.css', function(err, css){   response.writehead(200, {'content-type': 'text/css'});   if(err) response.write('index nao encontrado');   response.write(css);   response.end(); }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -