Build web application

Create the client app

  1. Create new directory.
mkdir my_webapp
cd my_webapp
  1. Then we can initialize the Node.js project.
npm init -y

Create Express app

  1. Install Express.
npm install express
  1. After running this command, we will see the dependency appear in the package.json file. Additionally, the node_modules directory and package-lock.json files are created.

Build application

  1. Create new file name app.js.
touch app.js
  1. Select file app.js and paste this code. Then save it.
var express = require('express');
var app = express();
var fs = require('fs');
var port = 8080;

app.listen(port, function() {
console.log('Server running at http://127.0.0.1:', port);
});

Build application

Create a REST API

  1. Add the following code in the app.js file.
var express = require('express');
var app = express();
var fs = require('fs');
var port = 8080;
/*global html*/

// New code
app.get('/test', function (req, res) {
    res.send('the REST endpoint test run!');
});

app.get('/', function (req, res) {
html = fs.readFileSync('index.html');
res.writeHead(200);
res.write(html);
res.end();
});

app.listen(port, function() {
console.log('Server running at http://127.0.0.1:%s', port);
});

Create HTML content

  1. Create file name index.html.
touch index.html
  1. Select file index.html. Then paste the code below and save.
<html>
    <head>
        <title>Elastic Beanstalk App</title>
    </head>

    <body>
        <h1>Welcome to the FCJ Workshop</h1>
        <a href="/test">Call the test API</a>
    </body>
</html>

Build application

Running the code locally

  1. Select file package.json.
  2. Update package.json with script and save.
"scripts": {
    "start": "node app.js"
},

Build application

  1. In terminal, run command.
npm start
  1. Go to the link that appear to see the result.

Build application