NodeJS API-Part 1 / Starting our server using express
This is the 1º tutorial of a complete series of building a production “NodeJS API using Express, MySQL and Sequelize”.
Full source code of this class is available at GitHub Martial Arts API Part 1
Before we begin you should install the following things on your computer.
- Visual Studio Code
- NodeJS or How to install NodeJS MacOS or How to install NodeJS Windows
- MySQL for windows or MySQL for MacOS
If you get stuck in any of the installation you can google it to keep going with the tutorial.
NOTE: This tutorial will cover all the API being built based in a MySQL database. If you are using MongoDB or another I recommend you to check Academind Building a Restful API with NodeJS.
START OUR PROJECT
- Open any folder in Visual Code;
- Open the terminal and run the following line to start your project.
npm init
You can skip all the question or answer any of them if you want.
3. NodeJS will create a file for you called package.json
This file will contain basic information about your project and also it is there where you will check the libraries/packages you download from the internet.
INSTALL EXPRESS
Now we will add express to our app so we can start our server up and running.
- Install express.
- You just need to type on your Visual Code terminal the following line:
npm install express
3. express library will be added to your package.json
and a new folder called node_modules
and a file package-lock.json
will be added to your project.
RUN OUR EXPRESS APP
In our final step we will run our express app, for that you need to:
- Create a file name
app.js
and write the following code in this file.
In the first line you import express to your file. In the second line you just create a variable which will hold your express app and then in the last line you start it on port 8000.
To start your server, type in the terminal
node app.js
2. Now go to any browser and type localhost:8000
and you will see a message, which does not mean anything but it proves your app is running.
More exciting things will come in the next tutorials.
Full source code of this class is available at GitHub Martial Arts API Part 1