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.
- Create a new Node.js Repl project
- Name it "CRUD App"
- Rename index.js to app.js
- In the Files area, select Show hidden files

- In the .replit file, change the
entrypointfrom"index.js"to"app.js" - For test purposes, add a
console.log('hello')statement to app.js - 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.
- Require the
expressmodule, and store it in aconstnamedexpress - Create
constvariables forhostnameandport - Use
express()to create theappobject - Define a function named
getHomePagethat takesrequestandresponseas parameters, and sends a simple HTML "Hello World" message as a response - Set up the
/route to respond with thegetHomePagefunction - Define a function named
listenCallbackthat logs a "Server Running" message - Use
app.listenand pass in theport,hostname, andlistenCallback - 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
- In the project folder, create a new folder named "views"
- In the "views" folder, create a new file named index.ejs
- For now, put
<h1>TEST</h1>in index.ejs for testing purposes - In the app.js file, import the
ejsmodule and set the app's view engine to EJSrequire('ejs'); app.set('view engine', 'ejs'); - For now, in the
getHomePagefunction, replace theresponse.sendstatement withresponse.render('index'); - 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.
- In the project folder, create a new folder named "routes"
- In the "routes" folder, create a new file named index.js
- In the index.js file, set the
module.exportsproperty to an empty object - Add a property named
getHomePageto themodule.exportsobject, and set its value to a function - Make the function the same as the
getHomePagefunction from app.jsmodule.exports = { getHomePage: function (request, response) { response.render('index'); } }; - In the app.js file, remove the
getHomePagefunction definition - In the place of the
getHomePagefunction definition, userequireto pull in the index.js moduleconst index = require('./routes/index'); - In the
app.gethomepage route, replacegetHomePagewithindex.getHomePage - 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.
- In the project folder, create a new file named db.js
- In the db.js file, require
@replit/database, and store it in a const namedDatabase - Under that, require
uuid, and store it in a const nameduuid - Under that, create a new variable
dband set it to anew Database()call - Set
module.exportsto 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.