Let's understand the Express.js.
Need of Express.js or any other framework.
Before starting the article I want to discuss some of the concepts, How websites or web applications work, what is server side and fronted and so, on.
- User Ex- You Write a URL in your browsers by connecting with the internet in border term connect the internet and start searching for the website link, then you gate a website in your Brower window. This process is that simple it looks like ya practically if you understand it is easy, let us understand how,
- user Requests for the response, that is what we write in the URL.
- That URL is checked by DNS for its corresponding IP.
- The server side may generate dynamic content, for example, any social media app, or the server sends the HTML, CSS, and JavaScript file as a response, and sometimes we only exchange data between the user and server side firmly we call it JSON format.
- So as we have processed data on the server side and generated dynamic content into it,
(Server is nothing just a computer system connected to the internet),we need some server-side programming language Here, come some of the languages(JavaScript,python,Java,PHP),
Hold On JavaScript how as we know js it only executes on the browser side, yes my friend you are correct at the initial stage it does like that, but now day what we have done is just extract the js engine from the Browser example (v8 engine) and make it as the stand-alone entity that why now we use JavaScript in the server side as well (Which Is Node.js)
Node is a run time environment, based on the google v8 engine.
Now I think it gives a sense of how we are using js on the server side, Again we are talking about the Java js python ( then what this Framework is like (flask,sprig,express so on)), Basically framework is just your friend who cares for you and do all your inefficient work so that you can study well, Technically its mean frame book provide some for the package utility function and the set of rule how to use it.
- Example Node also provides an HTTPS server let find it how.
const http = require('http')
const hostname = '127.0.0.1'
const port = 3000
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Hello World\n')
})
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
})
Mark here to create a server only we have to do every task manually setting the status code, generating the server, routing, etc (connect with the dot here comes to our best friend who helps us to do some work here all the manual work done by the express and as a developer, we have a focus on the logic part only.
As we have understood the basics let's understand the basics of Express.js.
- The express.js is the fast, unopinionated, minimalist web framework for Node.js, the line is well understood except for the unopinionated which means there is more than one way to implement the same task as simple as it is.
- express Simple create an HTTP server for you where you will run your application ex- localhost:8000(port)/.
- Step to create a Simple express app.
- Install Node on your system.
- Make a directory of your choice.
- Init node in the directory and fill in the parameter version name etc.
- you will get a package.json file.
- create a file name as index.js.
- in Which we will import the express and start the server(before starting the server import it and install using npm install express.
Start the server node file name and see the output in the (localhost:port/endpoin).
Thank You for Reading this Article, Hope You Find it Helpful❤️