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$
Part of Web servers
- Create your serverP
- Create your appP
- Node and the terminalP
- More about the terminalP
- Web frameworksP
- Using ExpressP
- Serving static files
- Views and templates
- Routing
- Real data
- Includes
- Get confident with Express
- Build your app in ExpressP