DEV Community

Mahmoud Ashraf Mahmoud
Mahmoud Ashraf Mahmoud

Posted on

Stop repeating work and Start coding your express API ๐Ÿš€

Too much wasted time ๐Ÿ™

Think about every time you started a new Express API. you start by creating a new folder, initialize npm, installing the essential dependencies one by one, creating the same folder structure with the same files with the same default code, and making a ton of config files for eslint, prettier, git, etc. All of that and you didn't even start writing any code for your project. setting up express project takes a lot of time upfront and it gets more frustrating if you are working with microservices as you will go by these same steps more and more. I tried to deal with this problem by creating a pre-configured project in a repository and clone it every time I wanted to create a new project, but I found myself still editing it every time to meet the requirements of the new project. I took some time doing this then I realize that this isn't an ideal solution. So, I decided to do what's called a programmer move and automate the process.๐Ÿ‘จโ€๐Ÿ’ป

the Solution ๐ŸŽ‰

To solve this problem, I started to work on a package called new-express-app to create ready to code express apps with simple cli command. the package has to be different from the repository solution as it should create the project with my exact needs no more no less. So, I used the Inquirer.js package to ask the user a series of questions about his needs. and here is the questions:

  • Enter Project Name:

  • Enter version:

  • Enter description:

  • Enter author name:

  • Want to initialize git?

  • Want to install dotEnv?

  • Want to install database driver?

  • Want to initialize eslint/prettier?

the first 4 questions to create the package.json file and the rest to determine which dependencies/devdependencies to install. the package installs some dependencies by default which are:

and you can optionally add these dependencies:

the package uses the child process exec to initialize git, install dependencies and even open vscode for you. also it adds all the config files for eslint, prettier, dotenv and git. and the required code for database connection is also added so you only need to add your connection string and start coding immediately ๐Ÿš€. you can refer to the GitHub repository to see the created folder structure and the files content.

let's some up ๐Ÿ˜Š

  1. Install new-express-app globally.
npm i -g new-express-app
  1. Answer the questions Imgur
  2. You are ready to change the world.๐Ÿ’ช Imgur

Hope you like it and if you have any ideas to improve please, leave it in the comments ๐Ÿ˜€ .

Have time to help? ๐Ÿค

If you like the idea and you have the time to help feel free to go to the GitHub repository and add the functionality that you want.

Top comments (28)

Collapse
 
ssimontis profile image
Scott Simontis

I feel like Express is a framework for writing web frameworks. It is so low-level and every method becomes so repetitive, that unless you or your company have a solid library built on top of it already, it is one of the most unproductive solutions for a server.

For really simple CRUD apps, I think it would be cool to see a database schema transformed into a REST API. Not applicable most of the time, but totally automated when required.

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

I'm pretty new to node.js with less that a full year of experience. and we I get started I found express is the most used library with node.js when it comes to building REST API's. so I started using it and I did't expose to any other alternative. and I guess Google Firebase implements the idea that you are taking about, but it has a lot of limitations. I see it with express like you trade the simplicity for the more functionalities you get over something like FireBase.

Collapse
 
stagfoo profile image
Alex King

I think there are some solutions that do just that if you look around, you can also try json-server for really simple apps

Collapse
 
aghost7 profile image
Jonathan Boudreau

Yea, there are more complete frameworks like eggjs and nest.

Collapse
 
joserocha3 profile image
Pablo Rocha • Edited

Hasura.io does almost that. Creates a graphql API from your postgres schema.

Collapse
 
drazisil profile image
Molly Crendraven

Thanks for the article. This is motivating me to look at yeoman again and make a template. You are correct, so much repeated time wasting work to start a new project.

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

Yup, it seems like 5 or 10 mins but in really it becomes too much when the number of projects increases. I didn't know about yeoman before but it seems like a pretty good tool and it would've been saved a lot of time with me, but it was fun coding it from scratch ๐Ÿ˜…

Collapse
 
drazisil profile image
Molly Crendraven

In the end, the fun is what really matters :high-five:

Collapse
 
chukwuemekaigbokwe profile image
Chukwuemeka Igbokwe

There is already a framework for that. You can check out Expresswebjs github.com/Alexigbokwe/ExpressWebJs

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

I took a look on the repo and I see a great work and effort with pretty good ideas โค๏ธ. And I see that you are the author so if you don't mind we can work together to build a better library to server this purpose๐Ÿ˜€

Collapse
 
mustaphee profile image
Yusuff Mustapha

+1 contributor, if you guys are willing to team up! I have also done something like this

Thread Thread
 
uptotec profile image
Mahmoud Ashraf Mahmoud

I'm ready whenever you are ready ๐Ÿค

Thread Thread
 
hanzlahabib profile image
hanzlahabib

Count me in..
Don't go through my repo ๐Ÿ˜…

Collapse
 
chukwuemekaigbokwe profile image
Chukwuemeka Igbokwe

That will be nice, we are happy to release the new version of Expresswebjs. You can check out the website at expresswebjs.com for documentation and use.

Collapse
 
zluther89 profile image
Zach • Edited

I could be wrong, but I don't think cors module is needed by default, I think the setup you have for cors is what express already does by default, the cors module is only needed if you need to make exceptions.

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

When I started using expess i spent a whole day trying to receive requests from the browser with react js. I was sending requests to the backend with postman and it works fine but it doesn't receive and requests from the browser. And then I found that to be able to receive browser requests you have to use the cors module. So I thought it would be convenient to add it by default as most of express API's will be consumed by some sort of a frontend which may work on a browser ๐Ÿ˜…

Collapse
 
zluther89 profile image
Zach

It depends on if the server is hosting the page or not. But yeah I see what you mean, makes sense! Really cool project ๐Ÿค—

Collapse
 
ralphkay profile image
Raphael Amponsah

Really helpful. Its a crazy orchestra with setp these days.

Collapse
 
miteshkamat27 profile image
Mitesh Kamat

Great effort !!!

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

Thanks ๐Ÿ˜€.

Collapse
 
spiritupbro profile image
spiritupbro

no i code web in c++

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

Coding isn't limited to one language and this is one of the reasons why I love coding so much. Pick the language that you want and do whatever you want. Maybe some languages are better in sertin fields, but you know it's doable with any language if you want. Personally, I chose JavaScript because, the same idea of having a language that works everywhere and JavaScript has a solid fremeworks/libraries on every feild. This isn't making it the best solution, but if you are like me working frontend, backend and even mobile development I guess having a one language to do all of them is a great thing to have ๐Ÿ˜…

Collapse
 
spiritupbro profile image
spiritupbro

yes sir i got you i just joking no way i use c++ as backend also

Collapse
 
michi profile image
Michael Z

It sounds like a lot of fun to build such a tool. Personally, I will just stick with Adonis though, it comes with most things you need for your average app.

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

Sadly, I didn't try it till now, but surely I'll give it a try soon. thanks ๐Ÿ˜€

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

thanks, it's now updated! ๐Ÿ˜ƒ

Collapse
 
uptotec profile image
Mahmoud Ashraf Mahmoud

Now in V 1.2.0 you can create Typescript project with TsLint and Prettier configuration ๐Ÿ˜€