Javascript backend with node and express

Jimmy Gam
5 min readSep 3, 2020

How to write backend with nodejs and express js

What is Node JS?

Nodejs is a javascript runtime built by Chrome V8 javascript engine. It is very light and efficient for using event-driven and non-blocking I/O models. It is following CommonJS modules and recently working on ES Module system but it is experimental. The one of the huge differences between Javascript in browser and Nodejs comes from the access of filesystem of Nodejs.

Nodejs Architecture(https://developer.ibm.com/technologies/node-js/tutorials/learn-nodejs-tour-node/)

As you can see, through the node runtime, the user is able to access node core and node API. Node API is a set of built-in modules provided by Node.js out of the box for you to build your applications such as filesystem(fs) apis. A node core is a set of javascript modules that implement the node api. Then this will be run by JS engine, V8 and javascript code will be compiled to the machine code. Through this architecture, now we are able to understand how much extent to javascript can be used. (according to it, very fit to the backend system)

Now, what is commonjs modules? Commonjs is a group with a goal of building up the JavaScript ecosystem for web servers. Basically, sets various rules for making JavaScript to be used in web servers. Some of the efforts are the following:

1. divide the scopes and create modules that manage dependencies

2. standard for File I/O, binary data and etc

3. standard for web server and a database

4. managing system that deals with apt of linux

Now, how do we come up with modules with CommonJS?

In order to use modules, we need to use a keyword require in a file of a different scope and in order to export the module, we can use module.exports. Here is the example of how:

circle.js that exports two functions
index.js that uses the functions imported from circle.js

As you can see, in exporting, you have to have an export object and by means, the only way to export something outside. In CommonJS, the keyword export is a reference to module.exports. Therefore, you should be very cautious of not declaring exports with some other definitions.

exports in Commonjs

So, we have explored modules but why do we need to go over these terms? In nodejs, we can manage external modules by using yarn and npm. For example, if I wanted to use some module called current clock that easily calculates current time for every region, you can simply use external modules called curtime by using npm or yarn.

What is express?

Express.js is a web framework for Node.js and allows us to run server easily by extending basic http modules and providing routing, middleware API and etc. Also, it allows us to render templates with various template engines.

How to create express server?

express.js that shows how to create express server

First you need to install express with npm install express or yarn add express in the terminal and easily import it by using require keyword. Then, you can assign to a variable with express() object and use get, post, put, delete http method API provided by the express. Lastly, using listen API, you can set which port to the app variable can take with callback function followed by it.

Express Routing?

router.js that shows express routing

Routing is defined as per specific HTTP request method or endpoint, sets the URI of the application.

Express middleware?

Express middleware acts as a software in between data request and response. Examples can be fixing the request with a proper inputs or log messages to which request and response acted upon on specific requests. The middleware stack can be briefly described as following:

Middleware stack diagram

We can set express middleware as the following code:

express middleware example code

We can use a keyword called “use” to register the middleware to be acted upon. As you can see, the next refers to continue operation where the middleware can proceed to the next middleware if exists or just go to the next operation specified in the file.

What is template engine?

Template engine refers to changing certain variables to the data dynamically. Most well known template engines are ejs, jade, and handlebar. ejs uses standard grammar of html so we will go with ejs.

setting the template engine as ejs

As you can see in the fourth line where it sets the path of the rendering files, we can put the following html files to render some data.

html file that renders the data dynamically

Finally, we can return the rendering pages by using render method.

with res.render, we are able to return data

Conclusion

Very speedy and easy to run a server with javascript using nodejs and expressjs. Basically we can use methods other than render to return data to the frontend frameworks such as react or vue in order to display the data on the frontend itself. I believe with javascript, these days, we can deliver a fullstack service with ease.

--

--

Jimmy Gam

Hello, I am a software engineer @Amazon. Any technical discussions are welcome so please contact me at jgam@alumni.nd.edu