Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 28, 2021 08:59 pm GMT

Create a backend in Javascript: 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

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:

$ nodeWelcome to Node.js v16.9.1Type ".help" for more information>
> console.log ('Hello World')Hello Worldundefined

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')

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.jsHello World

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).


Original Link: https://dev.to/ericchapman/create-a-backend-in-javascript-introduction-to-node-js-215a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To