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
entrypoint
from"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
express
module, and store it in aconst
namedexpress
- Create
const
variables forhostname
andport
- Use
express()
to create theapp
object - Define a function named
getHomePage
that takesrequest
andresponse
as parameters, and sends a simple HTML "Hello World" message as a response - Set up the
/
route to respond with thegetHomePage
function - Define a function named
listenCallback
that logs a "Server Running" message - Use
app.listen
and 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
ejs
module and set the app's view engine to EJSrequire('ejs'); app.set('view engine', 'ejs');
- For now, in the
getHomePage
function, replace theresponse.send
statement 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.exports
property to an empty object - Add a property named
getHomePage
to themodule.exports
object, and set its value to a function - Make the function the same as the
getHomePage
function from app.jsmodule.exports = { getHomePage: function (request, response) { response.render('index'); } };
- In the app.js file, remove the
getHomePage
function definition - In the place of the
getHomePage
function definition, userequire
to pull in the index.js moduleconst index = require('./routes/index');
- In the
app.get
homepage route, replacegetHomePage
withindex.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
db
and set it to anew Database()
call - 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.