# Serve static assets using express

Express has a built in functionality which can be used to serve static assets

```
const express = require("express");

const app = express();

app.use(express.static("public"))

app.listen("8082", function(){
    console.log("Server Started");    
});

```
Note that *public* is the folder at project level where all the static assets are stored. 

### Restrictive access to static assets

We can use middleware as below to have restricted access to the static assets.

```
const express = require("express");

const app = express();

function validate(req, res, next){
    //check for valid access
    if(isValid){
         next();
    }else{
        res.send(401, "Unauthorised");
    }
}

app.use(validate, express.static("public"));

app.listen("8082", function(){
    console.log("Server Started");    
});

```

