How To Speed Up Creating A New Express Project

・6 min read
How To Speed Up Creating A New Express Project

The problem with bootstrapping new projects

I really enjoy working with Node.js projects but starting a new one is a little annoying since most of the time I’m forced to repeat steps made in the previous projects.

I know that it's a one of Node.js benefits - the freedom given to developers - but ask for yourself, how often do you start a new project and want to experiment with a different authentication package or application structure?

Of course, you do it when you’re playing around, but when you have a deadline given by a customer you will not think about exploring a new options instead of using battle-tested tools.

I was looking for some kind of express.js project bootstrapper for quite a long time but I couldn't find any meeting my requirements. The second step I took, was to ask people at my company, what do they think about creating a bootstrapper that will everything they have ever wanted?

Do you find yourself in that paragraph?

I have great news for you!

After a few sleepless nights, I came up with the first version of working generator.

SurpiseJS to the rescue

SurpriseJS is a CLI Generator for creating basic express.js application with:

  • working mongoDB connection
  • prepared basic routes and CRUD functionality for a given models
  • JWT authentication
  • CORS setup

How to start out with SurpriseJS

Starting out with the generator is really easy, so it can be a good choice for express newcomers that are looking for some standardised project structure and code hints.

ALL YOU HAVE TO DO IS:

npm -g surprisejs

(Make sure that you have Node.js and Mongo installed on your device) Then you need to run surprisejs in your terminal. You will see a list of options.

What options SurpriseJS has?

Core - Generates a fully working Node.js application based on provided project and database name Auth - Adds login route with auth middleware which listens for specific prefix defined by the user during installation CORS - Adds CORS configuration to application Route - Generates route with a model for the provided model name and endpoint URL CRUD - Adds basic CRUD with filtering and pagination to the selected route

Example setup:

1. Generate base application structure with main files

  • Select core option
  • Provide project name and database name
  • Move to your project folder and continue.

2.1 Generate some routes for your application

  • Select route option
  • Provide model name and endpoint url ( or click enter for default value )

2.2 Generate nested routes

  • Select route option
  • Provide model name with slash ('/') and endpoint url ( or click enter for default value )

3. Generate default CRUD for selected route

  • Select crud option
  • Choose from multi-select on which route You want to add default CRUD

4. Add Authentication to application

  • Select auth option
  • Provide endpoints prefix

5. Add CORS functionality

  • Select cors option
  • Choose between Basic and Advanced configuration
  • No matter what you choose, SurpriseJS will install required NPM and add configuration to app.js

6. Checking if application works:

  • Type npm start in project directory
  • Open Postman or other API development tool.
  • Type in url adress: http://localhost:3000/api/users while api/users is based on your defined routes.
  • If you have route and CRUD defined for specific endpoint, You should see empty array. Now let's fill it with some data.
  • Select method POST and as Body use raw option with JSON language. In this way send empty object {}. This will return object with:

    • _id,
    • createdAt,
    • updatedAt,
    • _v

Now retry previous url ( http://localhost:3000/api/users ) and You should see array with one object. If You want to add keys and values to model, just move to models/ folder , select one of created models and add new key like inside file example shows

Future plans

Future steps:

  • An important part of every developer's work should be testing the code. That’s why I would like to prepare basic tests with examples of how to deal with them, statistics about coverage and the number of tested lines per file
  • Typescript is gaining popularity and is more often used in new projects. With variables types, interfaces and classes, it's easier to write predictable and secured code. It also requires more knowledge to start working with that type of application. We want to give a choice if user wants to use Javascript or Typescript on his project. Every option will be prepared for both parts if possible
  • Instead of using Express, a lot of people are using Koa.js. It has a few big advantages, one of them - generators. Generators aren’t available in Express, so that’s a great opportunity to test new framework. Koa is also more modular. It doesn’t have, for example, routing inside its core. It will make my generator more detailed to user requirements. You don’t want routes and need to have as small as possible application ? No problem - generate Koa app instead of Express.
  • Almost every application has something to do with files. Avatars, photos, documents, all of them are crucial. You can use different options to save them:

    • Amazon S3
    • Firebase Storage
    • Local directory

I want to allow you to choose your best option, with a few steps, clicks, selects. You could check all of the options, customize them and decide what suits you best. No need to write a lot of code everytime you change your mind. Just change select in generator.

Summary

As said at the beginning, the idea was to improve the process of creating new Express application and help newcomers with basic setup. Now when you can generate a whole project in just few steps, clicks and minutes, you can focus on delivering business logic.

Let me know if you find a place to use SurpriseJS in your daily work with stars on GitHub!

If you see some missing functionalities, want to add some of above-mentioned or would like to improve them, feel free to create an issue or just create a pull request.

GitHub Profile: https://github.com/RobertMrowiec

Twitter: https://twitter.com/RMrowiec99

And if you're looking for a skilled development team, check out our developers

Related articles