CRUD App - Setup

Write code for a basic boiler-plate Express web server.

Getting Started

First, get a basic Node.js app up and running.

  1. Create a new Node.js Repl project
  2. Name it "CRUD App"
  3. Rename index.js to app.js
  4. In the Files area, select Show hidden files
  5. In the .replit file, change the entrypoint from "index.js" to "app.js"
  6. For test purposes, add a console.log('hello') statement to app.js
  7. Click the "Run" button to run the program, and make sure it works so far

Basic Web Server

Next, it's time to create the bare-bones Express server. Open the app.js file to begin.

  1. Require the express module, and store it in a const named express
  2. Create const variables for hostname and port
  3. Use express() to create the app object
  4. Define a function named getHomePage that takes request and response as parameters, and sends a simple HTML "Hello World" message as a response
  5. Set up the / route to respond with the getHomePage function
  6. Define a function named listenCallback that logs a "Server Running" message
  7. Use app.listen and pass in the port, hostname, and listenCallback
  8. Click "Run" to run the server, open the homepage, and verify that the "Hello World" message appears!

Code

// Imports
const express = require('express');

// Set hosting information
const hostname = '0.0.0.0';
const port = 8080;

// Initialize app
let app = express();

// Route function
function getHomePage(request, response) {
    response.send('<h1>Hello World</h1>')
}

// Set routes
app.get('/', getHomePage);

function listenCallback() {
    console.log('Server Running');
}

app.listen(port, hostname, listenCallback);

Organizing the Code

With a larger app, it can be helpful to organize the code in multiple folders and files. This makes it easier to maintain.

Views Folder - EJS

  1. In the project folder, create a new folder named "views"
  2. In the "views" folder, create a new file named index.ejs
  3. For now, put <h1>TEST</h1> in index.ejs for testing purposes
  4. In the app.js file, import the ejs module and set the app's view engine to EJS
     require('ejs');
     app.set('view engine', 'ejs');
    
  5. For now, in the getHomePage function, replace the response.send statement with response.render('index');
  6. Load up the homepage in a browser, and verify that the "TEST" message appears!

Routes Folder

With many different routes, it is much easier to organize functions in multiple files. To do this, it is necessary to use module.exports in the separate files, and require in the app.js file.

Module exports are the instruction that tells Node.js which bits of code (functions, objects, strings, etc.) to export from a given file so other files are allowed to access the exported code.

  1. In the project folder, create a new folder named "routes"
  2. In the "routes" folder, create a new file named index.js
  3. In the index.js file, set the module.exports property to an empty object
  4. Add a property named getHomePage to the module.exports object, and set its value to a function
  5. Make the function the same as the getHomePage function from app.js
     module.exports = {
         getHomePage: function (request, response) {
             response.render('index');
         }
     };
    
  6. In the app.js file, remove the getHomePage function definition
  7. In the place of the getHomePage function definition, use require to pull in the index.js module
     const index = require('./routes/index');
    
  8. In the app.get homepage route, replace getHomePage with index.getHomePage
  9. Load up the homepage and make sure it still works!

The folder structure at this point should look like this:

Creating the Database

The next step is to create the Replit DB, and prepare for the various functions that will be needed to interact with it.

  1. In the project folder, create a new file named db.js
  2. In the db.js file, require @replit/database, and store it in a const named Database
  3. Under that, require uuid, and store it in a const named uuid
  4. Under that, create a new variable db and set it to a new Database() call
  5. Set module.exports to be an empty object

The code in the db.js file should look something like this:

const Database = require("@replit/database");
const uuid = require("uuid");
let db = new Database();

module.exports = {

}

Note that this should not do anything (yet). It will be used in the future for all database actions.

Next Steps

CRUD - Create (GET)

results matching ""

    No results matching ""