: Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications.
-
Allows to set up middlewares to respond to HTTP Requests
-
Defines a routing table which is used to perform different actions based on HTTP Method and URL
-
Allows to dynamically render HTML Pages based on passing arguments to templates
Install express
npm install express --save- saves the installation locally in the node_modules
- create a directory express inside node_modules
Express version check
$ npm view express version
4.17.1Install modules along with express
npm install body-parser --save
npm install cookie-parser --save
npm install multer --saveexpress_tutorial/
├── package.json
├── public
│ └── css
│ └── style.css
├── router
│ └── main.js
├── server.js
└── views
├── about.html
└── index.html
{
"name": "express-tutorial",
"version": "1.0.0",
"dependencies":
{
"express": "~4.13.1",
"ejs": "~2.4.1"
}
}npm installserver.js
var express = require('express');
var app = express()
var server = app.listen(8000, function(){
console.log("Express server running on port 8000!")
})result
$ node server.js
Express server running on port 8000!- When opening the web server on port 8000, the text
Cannot GET/is displayed- why?
- Because the
Routerhasn't been set up yet!
- Because the
- why?
server.js
var express = require('express');
var app = express()
var server = app.listen(8000, function(){
console.log("Express server running on port 8000!")
})
app.get('/', function(request, response){
response.send('Hello World')
})It is good coding practice to write router code and server code in separate files!
- Create a router folder
- Create
main.js
router > main.js
module.exports = function(app){
app.get('/', function(request, response){
response.render('index.html')
});
app.get('/about', function(request, response){
response.render('about.html')
});
}module.exportsallows the router code to be imported and used as a module inserver.jssince it was written separately~!
- Create a views directory
- Create
htmlfiles
views > index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index</title>
</head>
<body>
<p>This is index page!</p>
</body>
</html>views > about.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=<device-width>, initial-scale=1.0">
<title>About</title>
</head>
<body>
<p>About... what?</p>
</body>
</html>Modify server.js
var express = require('express');
var app = express();
// Import the router module main.js and pass app to it
var router = require('./router/main')(app);
// Define the location of HTML so the server can read it
app.set('views',__dirname + '/views');
// Set the server to use the EJS engine when rendering HTML
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
var server = app.listen(8000, function(){
console.log("Express server has started on port 8000!!")
}).js,css,imagefiles used in HTML- To handle
Static fileson the Server, use theexpress.static()method!
- Create a public directory
- Create a css directory
- Create
.cssfiles
public > css > style.css
body {
background-color: black;
color: white;
}Modify server.js
// Set the public directory as the static folder
app.use(express.static('public'));Add CSS link to html files
<link rel="stylesheet" href="css/style.css">127.0.0.1:8000/
127.0.0.1:8000/about



