上QQ阅读APP看书,第一时间看更新
How to do it...
- Create a new directory named public
- Move into the new public directory
- Create a new file named index.html
- Add the following code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Simple Web Application</title> </head> <body> <section role="application"> <h1>Welcome Home!</h1> </section> </body> </html>
- Save the file
- Navigate back out of the public directory
- Create a new file named serve-static-assets.js
- Add the following code. Initialize a new ExpressJS application:
const express = require('express') const path = require('path') const app = express()
- Include the express.static configurable middleware function and Pass the path of the /public directory where index.html file is located:
const publicDir = path.join(__dirname, './public') app.use('/', express.static(publicDir))
- Listen on port 1337 for new connections:
app.listen( 1337, () => console.log('Web Server running on port 1337'), )
- Save the file
- Open a terminal and run:
node serve-static-assets.js
- To see the result, in your browser, navigate to:
http://localhost:1337/index.html