BETAThis is a new service – your feedback will help us to improve it.

Node and the terminal

Javascript was originally meant to run only on web browsers, but this isn't the case any more.

Node.js is a tool that allows you to write and run Javascript on the server as well.

We're going to be building our web app using Node and Javascript for several reasons:

  • There's no need to learn a second programming language
  • Javascript is easy and quick for beginners to work with
  • Tools like the GOV.UK prototyping kit are written in Javascript

Let's write a simple Javascript program in our Codeenvy workspace, and run it using Node.

When you created your app in the previous session, a folder was created with a file hello.js inside it.

Delete everything in this file so we can start afresh.

Then write a simple program:

console.log("This is a Node program")

You don't need to save the file. Codenvy will save your changes automatically.

Run a Node program

Let's run this program using the terminal.

If you just started Codenvy, your terminal should have only a single prompt in it, which will look something like:

user@6f43d9472d8d:/projects$

The /projects part of this prompt tells us that our terminal is currently looking in our projects folder.

The projects explorer is also inside this /projects folder - it's the highest folder we can access on Codenvy. ALl the code we write will be inside subfolders of this /project folder.

From looking at the projects explorer, we can see that the file we want to run is in a subfolder called my-app. We need to tell the terminal to look in this folder.

Type cd my-app and hit return.

The prompt should change to something like:

user@6f43d9472d8d:/projects/my-app$

Now run your program by typing node hello into the terminal and hitting return.

You should see your message appear in the terminal, with a new prompt appear below it:

user@6f43d9472d8d:/projects/my-app$
This is a Node program
user@6f43d9472d8d:/projects/my-app$
Make sure your program runs successfully before continuing.
Lessons last updated 12th July 2019. You can improve this lesson on Github.
Part of Web servers
  1. Create your serverP
  2. Create your appP
  3. Node and the terminalP
  4. More about the terminalP
  5. Web frameworksP
  6. Using ExpressP
  7. Serving static files
  8. Views and templates
  9. Routing
  10. Real data
  11. Includes
  12. Get confident with Express
  13. Build your app in ExpressP