DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Create a backend in Javascript (part 1): Introduction to Node.js
Eric The Coder
Eric The Coder

Posted on • Updated on

Create a backend in Javascript (part 1): Introduction to Node.js

Here is a series of articles to help you create backend applications in Javascript.

Node.js is now a must, so it is essential for a developer to master it.

I will publish a new article every two days and little by little you will learn everything there is to know about Node.js

To not miss anything follow me on twitter: https://twitter.com/EricTheCoder_


What is Node.js?

NodeJS allows you to run JavaScript code outside of a browser.

Node.js is a free, open source, server environment that uses JavaScript language to create server-side web applications. (backend)

Node.js works on different platforms (Windows, Linux, Unix, Mac OS X, etc.) With Node.js we can build a fast and highly scalable web application.

Using Node.js also means that we can use JavaScript across the stack, i.e. the same language for the frontend and the backend. So more rapid and efficient development.

Node.js has a huge library of ready-made packages that will save you a lot of time. These libraries are managed by NPM (Node Package Manager)

A special feature of Node.js is that it uses asynchronous programming (running multiple tasks at the same time) vs synchronous programming (running one task at a time) found on many server-side programming languages like PHP and Ruby.

Installing Node.js

Node.js can be installed directly from its website: [https://nodejs.org/en/ marge(https://nodejs.org/en/)

Detailed documentation is also available on the Node.js website: [https://nodejs.org/en/docs/ marge(https://nodejs.org/en/docs/)

Once the installation is complete, you can check the version installed with this command

$ node -v
# v16.9.1
Enter fullscreen mode Exit fullscreen mode

Hello World

Tradition requires, the first thing we are going to do is the classic 'Hello World'

The fastest and easiest way to run code with Node is by using REPL. To launch the REPL, just run this command:

$ node
Welcome to Node.js v16.9.1
Type ".help" for more information
>
Enter fullscreen mode Exit fullscreen mode
> console.log('Hello World')
Hello World
undefined
Enter fullscreen mode Exit fullscreen mode

The REPL allows you to run javascript but you will understand it is very limited. The REPL is used for running small orders or for testing only.

If you want to write a complete program in NodeJS, you will need to create a file and run it.

Create and open the app.js file, enter the following line:

console.log('Hello World')
Enter fullscreen mode Exit fullscreen mode

Each file is considered by NodeJS to be a module and can therefore be executed.

To do this, from the terminal enter: node

$ node app.js
Hello World
Enter fullscreen mode Exit fullscreen mode

There you go, well done you have just created your first NodeJS application!

Whenever you need to run NodeJS code you will need to do so with this command.

We will see, later that there is a way to create a NodeJS server that will allow code to be executed automatically.

JavaScript browser vs JavaScript server

As you have seen thanks to NodeJS you can run JavaScript code without a browser! This is good but despite this, not everything is exactly the same.

Here is a list of the main differences between JavaScript browser code and NodeJS

  • A browser application run on the client's computer
  • A NodeJS application is running on the server
  • With NodeJS, there is no browser so no DOM and no Window object
  • With NodeJS it is possible to access the file system
  • With NodeJS, the module system called 'CommonJS' does not work with the same syntax as ES6 JavaScript modules
  • There are objects in NodeJS which are available everywhere in your code. These objects are called the Globals.
    • Here are some of these objects. You will learn how to use it in due course:
      • __dirname (return the path of the current folder)
      • __filename (returns the name of the file being executed)
      • require (allows you to load modules)
      • module (returns info on the current module)
      • process (returns info about the current environment)

Conclusion

That's all for today, follow me on twitter: https://twitter.com/EricTheCoder_ to be notified of the publication of the next article (within two days).

Top comments (2)

Collapse
w3nl profile image
Pieter EpeΓΌs Wigboldus

I don't know if you know that node.js is moving from commonjs (require) to JavaScript modules (import).
For node.js i use it already, in projects and packages, and I recommend it also to you to start with the migration.
Now in node.js 16 commonjs is not recommend anymore.
In node.js 18 you receive warnings that it will be deprecated.
In node.js 20 it will be deprecated. (2023?)

You can set the type to module in the package.json so node.js understand it.

Collapse
adigson profile image
adigson

Good day folks! I needed to be sure before I committed my time into following... My quest to get a handy, fast and viable resource to help in building the backend of an already built frontend (for an online programming boot-camp), gets me in touch with Eric the Coder's series on Node.js for backend development. I'm asking, will this series worth my time? Will reading and replicating the info in it be able to scale the project at hand? Thanks.

Find what you were looking for? Join hundreds of thousands of developers on DEV so you can:

Β 
🌚 Enable dark mode
πŸ”  Change your default font
πŸ“š Adjust your experience level to see more relevant content