I am not getting that why should I use expressjs if I can do everything fine with just node I mean is there any special advantage or something which I cannot do without express
-
While this will probably be closed as opinion-based, I'll posit a few considerations: Why use ReactJS when you can do everything with JavaScript? Why use Babel everything it supports is transpiled to ES5 anyway, so _in theory_ you could just write it by hand? It's a matter of practicality-- the framework provides utility, brevity, organization, and syntactic sugar that would not be available without it. You _can_ accomplish what you want in pure JS in Node. But remember that it may take more typing and be less easily maintained by other devs. – Alexander Nied Sep 09 '20 at 04:17
2 Answers
Nothing says you have to use Express. If you want to write every one of your lines of code from scratch and not use any previously built code, you are certainly allowed to do that.
Several ideas on the kinds of benefits you get from Express:
- You can do many things faster with Express (way less time to write solid, tested code) using built-in features.
- You can use the vast library of middleware that is compatible with Express which will again let you accomplish things much faster.
- Well known framework for others to help work on your code. You're inevitably going to have to build some sort of internal framework yourself just so you don't repeat the same code over and over again. If anyone else ever needs to work on your code, they're going to have to learn your mini-framework and the way you've done things rather than already be up-to-speed with an already familiar Express way of doing things.
To give you a few examples of already written and tested middleware for Express or built-in features in Express, here are a few:
- Multer - Middleware for parsing and handling multipart/form-data.
- Body-Parser - Middleware built into Express for parsing single part bodies (as in form or JSON posts) in several formats.
- Cors handling
- Serving static files automatically.
- A built-in view scheme for using any number of template engines.
- Automatic parsing of route parameters, query parameters, regex route matching, etc... so you can define routes using parameters.
- Express-session - Express middleware for server-side session handling (such as user login).
- Morgan - Express middleware for logging http requests/responses.
- Passport - Authentication middleware for supporting all sorts of authentication such as Google, Facebook, Twitter, etc... along with various custom strategies.

- 683,504
- 96
- 985
- 979
No, there is not a single special advantage you will get by using an express framework which can not be done in nodejs. But since express is a framework it helps you in the following things
- To maintain the basic structure of your app.
- To give you an easier way to write much stuff like routes, middleware, and many more.
- To handle the views.
- and many more.
To experience it just try to create a basic REST application only using nodejs and then try to create the same using express, you will see writing the application in express will save a lot of time, plus give you much cleaner code since many things will be handle by express itself.

- 264
- 1
- 9