1

I import jQuery, boostrap in my head like this

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"</script>

I'm trying to use NodeJS and Express like this, I found it on Google

var http = require('http');
var fs = require('fs');
var express = require('express');
var app = express();

http.createServer(function(req, res) {
fs.readFile('main.html', function(err, data) {
res.writeHead(200, {
  'Content-Type': 'text/html',
  'Content-Length': data.length
});
res.write(data);
res.end();
});
}).listen(8000);

But when my page is loaded, those libs are not loaded. I also have some images

    <div class="item active">
          <a href="vungtau.html">
          <img src="IntroduceImage/VungTau.jpg" alt="Vũng Tàu">
          <div class="carousel-caption">
            <h3>Vũng Tàu</h3>
            <h4>Lần đầu đi Vũng Tàu bằng xe máy</h4>
          </div>
          </a>
        </div>

They are not loaded too. This is address: D:\Atom Project\IntroduceImage\VungTau.jpg

Anyone know why? It I load it without NodeJS like this file:///D:/Atom%20Project/main.html. They're all loaded. Please help, Im new to NodeJS.

enter image description here

3
  • 1
    go through express documentation there is express.static to configure root path of application then according to that relative path give the files refrence Commented Mar 7, 2018 at 6:19
  • Please refer this link to include files using NodeJS + Express into your project. Commented Mar 7, 2018 at 6:25
  • Thank you, @ManpreetMatharu, compare with your link, and tried some search. I did it. Commented Mar 7, 2018 at 6:33

2 Answers 2

1

You have to give file reference path as given in express.js-staticFile

something like,

const path = require('path');

app.use(express.static('./'));
app.use(express.static(path.join(__dirname, 'public')));

I have my images stored in the root directory of the server.

Sign up to request clarification or add additional context in comments.

3 Comments

Does it really need "pp.use(express.static(path.join(__dirname, 'public')));"? I only use "app.use(express.static('./'));", it works fine
if you have an external file in public directory so,
@NguyenHoangVu-K11FUGHCM Welcome
0

hint as follows

app.use('/static', express.static(path.join(__dirname)));

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html

go through this link

https://expressjs.com/en/starter/static-files.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.