Welcome back! In the last lesson, you learned how to set up the main routes for your word prediction game using Express.js. Now, it’s time to bring everything together. In this lesson, you’ll learn how to create the server.js file, which acts as the main entry point for your Node.js Word Play Game.
The server.js file is important because it connects all the parts you’ve built so far — your routes, your database, and the Express app itself. Think of it as the “conductor” that makes sure every part of your game works together smoothly. By the end of this lesson, you’ll know how to set up this file so your game can run as a real web server.
Let’s quickly remind ourselves of what you’ve already built:
- Routes: In the previous lesson, you created routes that handle requests from the frontend, such as getting the daily prompt, submitting guesses, and showing the leaderboard.
- Database: You also set up a database to store player scores and manage the leaderboard.
Now, you need a main file that will:
- Import these routes and the database setup.
- Create an Express app.
- Make sure everything is ready before starting the server.
This is exactly what the server.js file will do.
Let’s build the server.js file step by step. We’ll start from scratch and explain each part as we go.
First, you need to import the modules that your server will use. These include:
express: The web framework for Node.js.path: A built-in Node.js module for working with file and directory paths../db: Your database setup file../routes: The routes you created in the previous lesson.
Here’s how you import them:
Explanation:
require('path')lets you work with file paths in a way that works on any operating system.require('express')brings in the Express framework.require('./db')imports your database setup, specifically theensureSchemafunction, which makes sure your database is ready.require('./routes')brings in all your game routes.
Next, you need to create an Express app and set up some middleware. Middleware are functions that run before your routes handle a request.
Explanation:
const app = express();creates a new Express application.app.use(express.json());tells Express to automatically parse incoming requests with JSON payloads. This is important because your game will send and receive data in JSON format.app.use('/static', express.static(path.join(__dirname, 'public')));serves static files (like images, CSS, or JavaScript) from a folder calledpublic. Any files in this folder can be accessed by the browser at the/staticURL path.
Now, you need to connect your routes to the Express app. This tells Express to use the routes you defined for handling requests.
Explanation:
app.use('/', routes);means that any request to your server will be handled by the routes you set up earlier. For example, if someone visits/leaderboard, Express will use the code you wrote in your routes file to handle that request.
Before your server starts listening for requests, you need to make sure the database schema is set up. This step helps prevent errors if the database isn’t ready.
Explanation:
ensureSchema()is a function that checks if your database has the right tables and structure. It returns a promise, so you use.then()to run code after it finishes.- Inside
.then(), you set thePORTandHOSTfor your server. If these aren’t set in the environment, it uses3000for the port and0.0.0.0for the host. app.listen(PORT, HOST, ...)starts the server and prints a message to the console when it’s ready.- If there’s a problem with the database, the
.catch()block prints an error and stops the server from starting.
Sample Output:
This message means your server is up and running!
In this lesson, you learned how to create the server.js file, which acts as the main entry point for your Node.js Word Play Game. You saw how to:
- Import the modules you need.
- Set up the Express app and middleware.
- Attach your routes.
- Make sure the database is ready before starting the server.
With this setup, your game server is ready to handle requests and connect all the features you’ve built so far. In the next practice exercises, you’ll get hands-on experience by building and testing your own server.js file. This will help you see how all the pieces work together in a real project. Good luck!
