Web
Analytics
Express JS Static files serving | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Initial command to start a new node application.

npm init
npm install express --save

Create folders public, images, and styles in the application.

Index.js

var express = require('Express');
var path = require('path');
var app = express();

app.use(express.static(__dirname+"/public"));
// to set simple path
// app.use(express.static(__dirname+"/images"));
// To set virtual path prefix
app.use("/myimages",express.static(path.join(__dirname, 'images')));
app.use(express.static(__dirname+"/styles"));
console.log(__dirname);
app.get("/mydata", function(req,res){

   res.send('mydata is called');
})

app.listen(3000);







index.html

<html>
<head>
    <title>My First Page</title>
    <link href="home.css" rel="stylesheet" />
</head>
<body>
    <h2>Hello Worlds</h2>
    <img src="/myimages/nr.jpg" />

</body>
</html>

home.css

h2
{
    background-color: red;
}