Initializing an Express Project
Initializing an Express Node.js Project
Getting Started with Express#
So far, we have only worked on the frontend part of our project. In this module, we will switch gears and create the backend part of the project, which will be an Express.js application.
Express is a Node.js web application framework that handles the common needs of a backend server-side web application. The features that we will be using include backend routing, serving static files and authentication.
Our source code is organized by
backend directories. If you haven't already done so, create the
backend directory under the root
At this point, the overall project tree should look like this:
Next we will use
express-generator to scaffold the backend project. Change to the
backend directory and run the following:
You should see the following output:
As you can see, the above command created several directories and files for us to initialize a small Express project.
Have a look at the
package.json file that was just created. You will see that there are a few dependencies that we need such as
npm install to install these additional dependencies:
Adding Prettier and ESLint#
Just like we did on the frontend project, we can configure Prettier and ESlint for the back end and get the same benefits. In the root of the backend project, add a
.prettierrc.json file with the following rules. Feel free to customize these rules to your preference.
Likewise, create an
.eslintrc.json file with the following config which is needed for a Node project.
npx eslint --initis another good way to initialize and configure an
eslintrc.jsonfile. This command provides an interactive installer to set the various options.
After this, let's add the ESLint command to the
start script in
npx eslint ./. You can also add a separate lint script so that you can easily run ESlint directly with an
npm run command:
Give it a try from the command line by running
npm run lint:
Interestingly our ESLint rules have found a
no-unused-vars problem with our auto-initialized code. Linting rules are often strict and may seem annoying at times but the end result is better quality code. Let's try to embrace the rules and be thankful for them!