Searching for Pattern - Express.static & app.use()
Tags: node, webdev, css, javascript
Published: 2021-10-26 03:16:46



Fun thing about learning JS is that you have to find its pattern for yourself. There's always something new. 

## GOAL: SERVE CSS FILE USING EXPRESS

My file structure is as follows

```
-server.js
-views
    -index.html
-public
    -style.css

```

Then a beginner coder like me will think "to connect HTML file to css file (i.e. make the html wear the css style), we will have to set href of the link tag like this..."

```html
<link rel="stylesheet" href="../public/style.css">
```

Yeah, obviously! Public folder is one folder up so '..' is so obvious! 

### But what if we can do this on a single js file?

Now, forget the tag above and we will work with this href instead
```html
<link rel="stylesheet" href="/public/style.css">
```

and we can still connect html and css together. 

To start off, I will create a file in the root folder. And, I will give this file cliche name, **SERVER.JS**. We will set up express here. 

Again, beginner coder out there like me, we will *GET* stuff from server, and coat our browser with either 'hello world!' or send file

```javascript
app.get("/", (req, res) => {
	res.sendFile(absolutePath);
});
```

absolutePath is the following

```javascript
let absolutePath = __dirname + "/views/index.html";
```

guess what? We get index.html on our browser. Of course, don't forget to *listen* to the server. That being said, I don't know what that means, it is just generic term for beginner coder like me. 

This is what we will see in our infamous localhost 3000. 

![kakarot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sml5q2b3mzx684ge5m3g.png)

Quite dry, right? Let's paint it with css. 

```javascript

let public = __dirname + "/public";

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

```

__dirname is, by the way, absolute path of where the server.js file is located. So, if you add the public, that means you set up absolute path for the public folder. 

### Anyways, thought it would work? No, The index.html did not wear background color. Oh no! The only fun part of doing css is wearing html page with background color! How come? 

What is express.static? It means, 'Oh we will look into any file in that (folder)' In our context, 'Oh we will look into any file in that public folder'


But look at the href in the link, it is `/public/style.css`. The link was directed at a wrong directory. Can you see it? The problem. We have already visited `/public` folder from `app.use(express.static(public))` but then going to `/public` again does not make sense to server, which means it won't be able to find the css file from `/public/public/style.css`


Then, where is the css file as we have used the middleware (app.use())? It is right here. 

![urlurl](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kh09ooxbtacyg83szecx.png)

![stylecss](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0fk4abm18kigbfbacor.png)


middleware and express.static directed on the public folder directory let us find style.css from `/style.css`

Can we add something on the middleware so that we can make href from link tag work? 

Yes, absolutely, we can set up its path 

`app.use('/public', express.static(public))`

This means, 'I will add this route /public to find every static file in public folder'. 

So now, the route that html link tag is looking for is now there at `/public/style.css`

It is now wearing some coat. 

![styled](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10msvrijol8chv43l1et.png)
 


...... After all, as evidenced from here, NodeJs is not that simple. This is a lot to play with. hype !== easy. Let's keep playing.