As a full-stack web developer, it is important to know both front-end and back-end development when creating web applications. Before coming to Waymaker Digital I had some experience working with front-end web development however I had very little experience with back-end development, which meant I had to spend some time learning and developing skills with back-end technologies.
Node.js
Node.js is a JavaScript runtime environment that allows JavaScript code to be executed outside a web browser, in other words, it allows us to create a web server using JavaScript. Other programming languages can be used for a web server; however, JavaScript is the language of choice for Waymaker Digital.
Express
Express is a back-end framework for node.js that allows web servers and APIs to be made quickly and efficiently. Express is one of the most popular frameworks in node.js and has become a de facto standard in the industry.
Setting up the server
The first steps to creating a back-end server are to ensure the correct packages are installed. The two basic packages that should be installed are express and cors. As mentioned before express is important for web servers, and cors (cross-origin resource sharing) allows requests to come from web clients with different URLs.
const express = require(‘express’);
const cors = require(‘cors’)
const app = express();
app.use(cors());
app.listen(‘8000’, () => console.log(‘server is listening on port 8000’));
In the above snippet, we import both express and cors to use in the server. We then initialize the express framework and save it to a constant. Then we attach cors to it to enable cross-origin sharing. Finally, we use the built-in listen method, which takes in the port number as an argument, to listen to any connections to the server.
Setting up a database
There are quite a few different database systems one can use for a web application, such as MongoDB, Oracle, and MySQL, and each one has its logic and implementation. The database I chose to focus on is PostgreSQL, which is an open-source relational database that supports both SQL and JSON querying. Using an ORM (Object Relational Mapping) makes it easier to interact with the database from the code. The ORM I utilized is called Sequelize, which is a promise-based ORM for Node.js and can be used for multiple different database types.
{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
The first step to setting up the database is making sure it’s configured properly; the above section tells the sequelize CLI exactly how to connect to the database and must be given the correct credentials.
npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string
Above is the sequelize command to create a database model. Here we’re telling sequelize to generate a model and give it a name, in this case, “User”. We also pass through some attributes, database fields, where we tell it the name of the attribute and the data type. Running this command will create a js file that will contain the model information as well as a file that contains the migration. Migrations allow the database to transfer into a new state or a previous state, either adding or removing tables and fields.
Setting up API Endpoints
Endpoints are functions in the API that perform a specific task, such as fetching data or updating a database. These endpoints are accessed through routes on the client-side of the application. The routes are set up using URI’s on the client-side which match the corresponding path in the endpoint. An API endpoint structure has four parts: the app, which contains the express instance, the method, which is the HTTP request, the path, which is the chosen path that will match with the URI, and finally the handler, which is the function that will execute when the route is matched.
The basic API methods are as follows, GET, POST, PUT and DELETE, below are examples of each.
The method below is the GET method, which is used to pass information from the server to the client.
app.get('/', function (req, res) {
res.send('Hello World!')
})
The method below is the POST method, which is used to send information from the client to the server.
app.post('/', function (req, res) {
console.log(req.body)
})
The method below is the PUT method, which is used to update or replace information on the server, such as records in a database.
app.put('/user', function (req, res) {
res.send('Got a PUT request at /user')
})
The method below is the DELETE method, which is used to delete records from a database.
app.delete('/user', function (req, res) {
res.send('Got a DELETE request at /user')
})
Testing the Back end
The best tool I have found for testing my server is an application called Postman. Postman allows you to call specific API routes, send information to the server, and view any data received from the server. It is incredibly useful, especially if you don’t have a front-end or if the front-end is arduous to navigate.
A tool I found useful for testing and viewing the database is pgAdmin, which is a tool for viewing Postgres databases. It allowed me to view what data was being saved and how it was being saved, which made it easy to spot errors and problems and implement solutions.
Finally, you can simply test the back end using the front-end, using the application as intended to get the full experience.
Conclusion
That has been a basic introduction to back end development with node.js and express, all of which I learnt and used to create a CMS application. I used the methods described in this blog to set up my server, create a database to store blog posts and create API routes to allow the front end to interact with the back end and create, retrieve, update and delete blogs. I then used Postman and pgAdmin to test the application and ensure that it worked as intended.
WayMaker Digital provided the guidance, mentorship and resources to support my learning growth and organized hackathons centred around building a workable project for end users which helped to provide accelerated learning as well as provision of comprehensive learning resources to help upskill team members which were facilitated by Udemy for Business licenses and company engineers/teams. WayMaker Digital also helped craft a career roadmap/development plan to facilitate the growth of each team/member.
Kai Chapman-Harris works as a full-stack software developer at WayMaker Digital, helping clients to solve business and technology challenges.
WayMaker Digital provides technology consulting, product design and information products, that enable our clients to consistently thrive and innovate to meet user needs. The quality of our experiences and ideas set us apart when it comes to service delivery, customer experience design and product development.
Our team of consultants have built digital experiences for leading brands with complex business scenarios ranging from startup companies to public services and from telecoms to e-commerce; no project deliverable is too complex.
Our approach: we adopt human-centred & design thinking models to solve complex business challenges, which ensure our clients are on top of their business.
Do you have a question or an enquiry; you want to discuss a project or need a free consultation? Give us a shout here.