![]() app.listen( 5000, console.log( 'Server is running on port 5000')) ![]() listen() method which takes the port number and an optional callback function to run on successful execution. In the case of dynamic routes, accessing (since you've used /:name) of the request object will return the value of the dynamic route parameter ( name in this case.)įinally, to start listening to incoming requests on a port, you can use the. Inside the parenthesis, you can enter whatever text or HTML you want. In the second line, the response is made through the. These 2 parameters are automatically sent when you make a request. get() method of Express.js that takes in 2 parameters: the endpoint or route, and a callback handler function that takes requests and response objects as parameters. In both examples above, the first line represents the usage of the. This code handles all GET requests made to the root ( "/") path and responds with "Hello World!" app.get( '/', (req, res) => !`) The main aim of a web server is to respond to the requests coming in from different routes with the appropriate handler function. To use the Express.js package, you must first import and create an instance of it inside the server.js file like so: const express = require( 'express') Once completed, you can open the folder using a text editor or IDE of your choice and create a new file named server.js.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |