Our first big example will be to deploy a Node.js app to a production web server. We don't need to get specific with this particular code, other than to say that it returns "Hello World" in your browser. There is no need to understand this code at this point.
To get this up and running, create a new project directory, create a new file, install it's dependencies, and then run it locally to make sure it works.
Once we have this all done, our next lessons will be all about deploying this software.
This lesson assumes you have Node installed on your system. If not, read about doing that.
First, let's setup a project directory:
mkdir app
cd app
Next, let's init a node project:
npm init
It's safe to press Enter to all the questions.
Now, let's install a dependency for our Hello World project:
npm install express --save
Copy and paste this to your terminal and press Enter to create an index.js file:
cat > index.js << EOF
const express = require("express");
const app = express();
const port = 3000;
app.get("/", (req, res) => {
res.send("Hello World! %v%");
});
app.listen(port, () => {
console.log("Example app listening at http://localhost:" + port);
console.log("Press Ctrl+C to exit");
});
EOF
To test, run it:
node index.js
Example app listening at http://localhost:3000
Then, in another terminal tab run:
curl localhost:3000
Hello World!
If you see Hello World!, you've successfully built a Node.js Hello World App.