Serving static files
Web servers do two things:
- Serve static, unchanging files like images, videos and CSS files
- Serve dynamic content, created in real-time based on the user's request
We're going to start by making our Express app serve some static files, and then move on to dynamic content.
In the last lesson, we added this line to our server.js
file:
server.use(express.static(__dirname + '/static'))
This line tells Express to serve whatever is in the folder /static
.
Create that folder, and put a new file called test.html
inside it. Put some HTML in that file for testing.
Make sure your app is running if it isn't already.
Visit your app on the web, and then add /test.html
to the end of the URL. You should see your own HTML page.
Anything you put in the static folder can be visited on the web by putting the right path in the URL.
Later, we'll use this functionality to serve a CSS file. You might also want to store images like logos in here.
Try visiting preview URL on a different device, like a smartphone. You should be able to view your app anywhere, so long as it is running on Codenvy.
Next, we'll use dynamic content to build your app's user interface.
Part of Web servers
- Create your serverP
- Create your appP
- Node and the terminalP
- More about the terminalP
- Web frameworksP
- Using ExpressP
- Serving static files
- Views and templates
- Routing
- Real data
- Includes
- Get confident with Express
- Build your app in ExpressP