Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Rocking Some NodeJS

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

I know this course is aimed at using JavaScript in a browser. Even still... we need to talk about Node.js! If you haven't used it before, Node.js is basically JavaScript... that you execute on your server! In the same way that we can write a PHP file and run it... we can do the same thing with Node.js: write a JavaScript file, execute it from the command line, and see the output!

So why are we talking about it? Well, it's going to give us a really easy way to test out some of these new ES2015 features. And in the next tutorial on Webpack, we'll be working with it even more.

Creating a Simple Node.js Script

Step one for you is to install Node.js. Because I'm on a Mac, I've already installed it using Brew. If you don't have it installed, just check out their docs - it's a bit different on every system.

Once you're ready, you should be able to execute node -v from the command line. Ok, let's experiment! At the root of your project, create a new file called play.js... because of course, Node.js is JavaScript!

Inside, use the familiar console.log('OMG! Node is JS on the server!'):

2 lines play.js
console.log('OMG! Node is JS on the server!');

Now, how do we run that? Simple:

node play.js

Boom! And now we can start experimenting with new ES2015 features... without needing to constantly refresh the browser. Let's play a bit more with our arrow functions. Create a new variable called aGreatNumber set to 10:

8 lines play.js
var aGreatNumber = 10;
... lines 2 - 8

Then, call setTimeout() and pass it an arrow function. Inside, console.log(aGreatNumber):

8 lines play.js
var aGreatNumber = 10;
setTimeout(() => {
console.log(aGreatNumber);
}, 1000);
... lines 6 - 8

Delay that call for 1 second, and, at the bottom, just log waiting:

8 lines play.js
var aGreatNumber = 10;
setTimeout(() => {
console.log(aGreatNumber);
}, 1000);
console.log('waiting...');

Head back to the terminal and run that! It prints, waits and prints! Sweet! Now let's go learn about let and const!

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial uses Symfony 3. But, since this is a JavaScript tutorial, all the concepts work fine in newer versions of Symfony.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.2.0",
        "symfony/symfony": "3.2.*", // v3.2.14
        "twig/twig": "2.10.*", // v2.10.0
        "doctrine/orm": "^2.5", // v2.7.1
        "doctrine/doctrine-bundle": "^1.6", // 1.10.3
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
        "symfony/swiftmailer-bundle": "^2.3", // v2.4.2
        "symfony/monolog-bundle": "^2.8", // v2.12.1
        "symfony/polyfill-apcu": "^1.0", // v1.3.0
        "sensio/distribution-bundle": "^5.0", // v5.0.22
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.19
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "friendsofsymfony/user-bundle": "~2.0@dev", // dev-master
        "doctrine/doctrine-fixtures-bundle": "~2.3", // v2.4.1
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.1
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "friendsofsymfony/jsrouting-bundle": "^1.6" // 1.6.0
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.2
        "symfony/phpunit-bridge": "^3.0" // v3.2.2
    }
}
userVoice