4/1/2019 ExpressJS Quick Guide https://www.tutorialspoint.com/expressjs/express
4/1/2019 ExpressJS Quick Guide https://www.tutorialspoint.com/expressjs/expressjs_quick_guide.htm 1/52 Previous Page Next Page ExpressJS - Quick Guide ExpressJS - Quick Guide Advertisements ExpressJS - Overview ExpressJS - Overview ExpressJS is a web application framework that provides you with a simple API to build websites, web apps and back ends. With ExpressJS, you need not worry about low level protocols, processes, etc. Express provides a minimal interface to build our applications. It provides us the tools that are required to build our app. It is flexible as there are numerous modules available on npm, which can be directly plugged into Express. Express was developed by TJ Holowaychuk and is maintained by the Node.js foundation and numerous open source contributors. Unlike its competitors like Rails and Django, which have an opinionated way of building applications, Express has no "best way" to do something. It is very flexible and pluggable. Pug (earlier known as Jade) is a terse language for writing HTML templates. It − Produces HTML Supports dynamic code Supports reusability (DRY) It is one of the most popular template language used with Express. What is Express? What is Express? Why Express? Why Express? Pug Pug MongoDB and Mongoose MongoDB and Mongoose We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more 4/1/2019 ExpressJS Quick Guide https://www.tutorialspoint.com/expressjs/expressjs_quick_guide.htm 2/52 MongoDB is an open-source, document database designed for ease of development and scaling. This database is also used to store data. Mongoose is a client API for node.js which makes it easy to access our database from our Express application. ExpressJS - Environment ExpressJS - Environment In this chapter, we will learn how to start developing and using the Express Framework. To start with, you should have the Node and the npm (node package manager) installed. If you don’t already have these, go to the Node setup to install node on your local system. Confirm that node and npm are installed by running the following commands in your terminal. node --version npm --version You should get an output similar to the following. v5.0.0 3.5.2 Now that we have Node and npm set up, let us understand what npm is and how to use it. npm is the package manager for node. The npm Registry is a public collection of packages of open-source code for Node.js, front-end web apps, mobile apps, robots, routers, and countless other needs of the JavaScript community. npm allows us to access all these packages and install them locally. You can browse through the list of packages available on npm at npmJS . There are two ways to install a package using npm: globally and locally. Globally − This method is generally used to install development tools and CLI based packages. To install a package globally, use the following code. npm install -g <package-name> Locally − This method is generally used to install frameworks and libraries. A locally installed package can be used only within the directory it is installed. To install a package locally, use the same command as above without the -g flag. npm install <package-name> Node Package Manager(npm) Node Package Manager(npm) How to use npm? How to use npm? We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more 4/1/2019 ExpressJS Quick Guide https://www.tutorialspoint.com/expressjs/expressjs_quick_guide.htm 3/52 Whenever we create a project using npm, we need to provide a package.json file, which has all the details about our project. npm makes it easy for us to set up this file. Let us set up our development project. Step 1 − Start your terminal/cmd, create a new folder named hello-world and cd (create directory) into it − Step 2 − Now to create the package.json file using npm, use the following code. npm init It will ask you for the following information. Just keep pressing enter, and enter your name at the “author name” field. Step 3 − Now we have our package.json file set up, we will further install Express. To install Express and add it to our package.json file, use the following command − npm install --save express To confirm that Express has installed correctly, run the following code. ls node_modules #(dir node_modules for windows) Tip − The --save flag can be replaced by the -S flag. This flag ensures that Express is added as a dependency to our package.json file. This has an advantage, the next time we need to install all the dependencies of our project we can just run the command npm install and it will find the dependencies in this file and install them for us. This is all we need to start development using the Express framework. To make our development process a lot easier, we will install a tool from npm, nodemon. This tool restarts our server as soon as we make a change in any of our files, otherwise we need to We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more 4/1/2019 ExpressJS Quick Guide https://www.tutorialspoint.com/expressjs/expressjs_quick_guide.htm 4/52 restart the server manually after each file modification. To install nodemon, use the following command − npm install -g nodemon You can now start working on Express. ExpressJS - Hello World ExpressJS - Hello World We have set up the development, now it is time to start developing our first app using Express. Create a new file called index.js and type the following in it. var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send("Hello world!"); }); app.listen(3000); Save the file, go to your terminal and type the following. nodemon index.js This will start the server. To test this app, open your browser and go to http://localhost:3000 and a message will be displayed as in the following screenshot. The first line imports Express in our file, we have access to it through the variable Express. We use it to create an application and assign it to var app. How the App Works? How the App Works? We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more 4/1/2019 ExpressJS Quick Guide https://www.tutorialspoint.com/expressjs/expressjs_quick_guide.htm 5/52 This function tells what to do when a get request at the given route is called. The callback function has 2 parameters, request(req) and response(res). The request object(req) represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, etc. Similarly, the response object represents the HTTP response that the Express app sends when it receives an HTTP request. This function takes an object as input and it sends this to the requesting client. Here we are sending the string "Hello World!". This function binds and listens for connections on the specified host and port. Port is the only required parameter here. S.No. Argument & Description 1 port A port number on which the server should accept incoming requests. 2 host Name of the domain. You need to set it when you deploy your apps to the cloud. 3 backlog The maximum number of queued pending connections. The default is 511. 4 callback An asynchronous function that is called when the server starts listening for requests. ExpressJS - Routing ExpressJS - Routing Web frameworks provide resources such as HTML pages, scripts, images, etc. at different routes. The following function is used to define routes in an Express application − app.get(route, callback) app.get(route, callback) res.send() res.send() app.listen(port, [host], [backlog], [callback]]) app.listen(port, [host], [backlog], [callback]]) app.method(path, handler) app.method(path, handler) We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more 4/1/2019 ExpressJS Quick Guide https://www.tutorialspoint.com/expressjs/expressjs_quick_guide.htm 6/52 This METHOD can be applied to any one of the HTTP verbs – get, set, put, delete. An alternate method also exists, which executes independent of the request type. Path is the route at which the request will run. Handler is a callback function that executes when a matching request type is found on the relevant route. For example, var express = require('express'); var app = express(); app.get('/hello', function(req, res){ res.send("Hello World!"); }); app.listen(3000); If we run our application and go to localhost:3000/hello, the server receives a get request at route "/hello", our Express app executes the callback function attached to this route and sends "Hello World!" as the response. We can also have multiple different methods at the same route. For example, var express = require('express'); var app = express(); 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"); }); app.listen(3000); We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more 4/1/2019 ExpressJS Quick Guide https://www.tutorialspoint.com/expressjs/expressjs_quick_guide.htm 7/52 To test this request, open up your terminal and use cURL to execute the following request − curl -X POST "http://localhost:3000/hello" A special method, all, is provided by Express to handle all types of uploads/s1/ expressjs-quick-guide.pdf
Documents similaires
-
23
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Attribution requise- Détails
- Publié le Jul 10, 2021
- Catégorie Administration
- Langue French
- Taille du fichier 1.5392MB