0% found this document useful (0 votes)
2 views

Lecture06-Expressjs

Express.js is a minimal and flexible Node.js web application framework that supports building web and mobile applications. Key concepts include JavaScript fundamentals, RESTful APIs, routing, middleware, and best practices for project structure and API development. The document outlines setup instructions, basic code examples, and organizational tips for Express.js projects.

Uploaded by

baonguyen2623
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture06-Expressjs

Express.js is a minimal and flexible Node.js web application framework that supports building web and mobile applications. Key concepts include JavaScript fundamentals, RESTful APIs, routing, middleware, and best practices for project structure and API development. The document outlines setup instructions, basic code examples, and organizational tips for Express.js projects.

Uploaded by

baonguyen2623
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EXPRESSJS

NGUYEN Thanh Ban


What is Expressjs?

- Express is a minimal and flexible Node.js web application framework

that provides a robust set of features for web and mobile application.

- MEAN is a free and open-source JavaScript software stack for building

dynamic web sites and web applications.


What to know first?

 JavaScript Fundamentals (Objects, Arrays, Conditionals,


etc)
 Basic Node.js & npm (Node Package Manager)
 Restful APIs.
Restful API
A RESTful API is an architectural style for an application
program interface (API) that uses HTTP requests to access
and use data.
HTTP/HTTPs Response
Setup Environment
- Node –version (or node –v)
- Npm --version
- Visual Studio Code (in this lecture)
- Postman for the client call APIs
Setup Environment
 npm init –y
 npm i express
 npm i –d nodemon
Expressjs - Helloworld

var express = require('express');


var app = express();

app.get(‘/api/user', function(req, res){


res.send("Hello world!");
});

app.listen(3000);
Expressjs - Routing
The following function is used to define routes in an Express application
app.method(path, handler)
The examples:
app.get('/hello', function(req, res){
res.send("Hello World!");
});

app.post('/hello', function(req, res){


res.send("You just called the post method at '/hello'!\n");
});
Expressjs - CRUD
Designing Structure
Expressjs - Middleware
Middleware functions are functions that have access to the
request object (req), the response object (res), and the next
middleware function in the application’s request-response
cycle.
Expressjs – Best practices
 Always begin a node project using npm init.

 Always install dependencies with a --save or --save-dev. This will ensure that if you
move to a different platform, you can just run npm install to install all dependencies.

 Stick with lowercase file names and camelCase variables. If you look at any npm
module, its named in lowercase and separated with dashes. Whenever you require
these modules, use camelCase.

 Don’t push node_modules to your repositories. Instead npm installs everything on


development machines.

 Use a config file to store variables

 Group and isolate routes to their own file.


Expressjs – Best practices for APIs
test-project/
node_modules/
config/
db.js //Database connection and configuration
credentials.js //Passwords/API keys for external services used by your app
models/ //For mongoose schemas
users.js
things.js
routes/ //All routes for different entities in different files
users.js
things.js
app.js
routes.js //Require all routes in this and then require this file in
app.js
package.json

You might also like