Categories
Uncategorized

Node server with ES6

Today I was wondering how could I write ES6 code and run it on node so I could write my applications using always the same pattern, no matter if I’m writing Node backend code of React Front-end code.

As a matter of fact, it’s quite simple to convert your node applications to full ES6 using Babel. You just have to add some modules and change some configurations.

First of all, you need to install Babel. We’ll install Babel in the development dependencies since we won’t use it in production. You can do that using the command below:

npm i -D @babel/cli @babel/core @babel/node @babel/preset-env @babel/register

I also needed to install another babel related package in the dependencies, this time in the main dependencies:

npm i @babel/polyfill

You also have to create the Babel configuration file:

$ touch .babelrc

And add this configuration to it:

{
"presets": ["@babel/preset-env"]
}

To keep everything organized, I moved my app into a folder called src. That way we can have all the source code and final compiled code into separated folders. I also added some scripts to my package.json to run the build and run the server in development mode using nodemon.

There’s one more thing to change. I just could not use the bin/www script to run the server, so I just copied the file to the src folder and renamed it to server.js.

"scripts": {
"build": "babel src -d dist",
"start": "nodemon src/server.js --exec babel-node",
"test": "echo \"Error: no test specified\" && exit 1"
}

The build script runs babel and compiles all the files inside of src folder and saves them to dist folder.

The start script runs the server.js file inside src folder passing code thru babel-node to transpile it. This way you can test the code on src folder in real time. Every time you save a file it will recompile and restart the server.

One more thing, if you are using sequelize, it’s best to add another file to the root folder of your application with some sequelize configuration. That’s important because we have moved the source code of the application to the src folder.

Just create a file called .sequelizerc and add the following code to it:

require("@babel/register");
const path = require('path');

module.exports = {
'config': path.resolve('config', 'config.json'),
'models-path': path.resolve('src/models'),
'seeders-path': path.resolve('src/seeders'),
'migrations-path': path.resolve('src/migrations')
}

Now you just have to convert all of your code to ES6. The conversion is the easy part, you can start by changing all the require calls to import and all the module.exports do export.

If you want to take a look at the boilerplate, check out at github:

https://github.com/rcabarreto/nodeServerES6

Categories
Node

What is Node.js?

Node.js (or just Node) is a lean, fast, cross-platform JavaScript runtime environment that is useful for both servers and desktop applications. Node is an asynchronous event driven JavaScript runtime built upon Chrome’s V8 JavaScript engine. It’s designed to build scalable network applications.

That being the raw definition, let me clarify. Node.js enables you to write server side JavaScript. You may now be wondering, how? As you know, JavaScript is a language which runs in a browser. The browser’s engine takes JavaScript code and compiles it into commands. The creator of Node.js took Chrome’s engine and built a runtime for it to work on a server. Don’t get confused with the word runtime. It’s an environment where the language can get interpreted. So what do we have now? A way to write JavaScript on the back end.

Categories
Node

Reading and Writing files with Node.js

In this post I’m going to show you how we can read and write files on our local filesystem using Node.js.

Being able to read files from you local filesystem can be very useful and there’s a buntch of stuff you can do with it, for example read local Json files and load Spreadsheets to your program.

To start, just create a js file and name it whatever you want. Then open this file in your editor of choice.

Reading from files is done via the core fs module. The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.

Categories
Express.js Node

Node.js & Express.js fundamentals

What is Node.js

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

As an asynchronous event driven JavaScript runtime, Node is designed to build scalable network applications. This is in contrast to today’s more common concurrency model where OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node are free from worries of dead-locking the process, since there are no locks. Almost no function in Node directly performs I/O, so the process never blocks. Because nothing blocks, scalable systems are very reasonable to develop in Node.