02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Setting up the server

Setting Up a Server for Full Stack Development

In this tutorial, we will set up a server for a full-stack application using Node.js and Express. This foundational step will help us integrate our front-end and back-end, bringing together everything we've learned so far in the course.

Prerequisites

Before we get started, make sure you have Node.js and npm (Node Package Manager) installed. This will allow us to create our server and manage dependencies.

Step 1: Initialize the Project

  1. Create a New Folder: Start by creating a new folder for this project. Inside your terminal, navigate to the desired location and create a folder named fullstack-server.

    mkdir fullstack-server
    cd fullstack-server
    
  2. Initialize npm: Use npm to initialize the project. This will create a package.json file that helps manage project dependencies.

    npm init -y
    

    The -y flag automatically answers "yes" to all prompts, setting default values for the package.json file.

Step 2: Set Up the Folder Structure

  1. Create Folders: Create a folder named public inside your project. This folder will contain all static files, such as CSS or images, that your server needs to access publicly.

    mkdir public
    
  2. Move Assets: Move any relevant CSS or JavaScript files into the public folder.

Step 3: Install Dependencies

Next, we need to install a few essential packages for our server:

  • Express: A fast, minimalist framework for Node.js.
  • MongoJS: A Node.js library for interacting with MongoDB (optional, as per personal preference).
  • Body-Parser: Middleware for handling incoming request bodies.

Use the following command to install these packages:

npm install express mongojs body-parser --save

Step 4: Create the Server File

  1. Create a Server File: Create a file named server.js inside the newly created source folder. This will be our main server file.

  2. Add Constants: Let's start by importing the modules we installed and initializing Express.

    const express = require('express');
    const mongojs = require('mongojs');
    const bodyParser = require('body-parser');
    const app = express();
    

Step 5: Set Up Middleware

  1. Static Folder: Make the public folder available as a static directory for the server. This means any files inside the public folder can be accessed directly by visiting the appropriate URL.

    app.use(express.static('public'));
    
  2. Body-Parser Setup: Configure the middleware to handle URL-encoded data, ensuring we can access data from POST requests.

    app.use(bodyParser.urlencoded({ extended: true }));
    

Step 6: Define Routes

Now, we'll set up a couple of basic routes.

  1. GET Route for Registration: We'll start by setting up a simple GET route to return a "Got it" response when visiting /register.

    app.get('/register', (req, res) => {
        res.send('Got it');
    });
    
  2. POST Route for Registration: Set up a POST route that will handle data submissions for our registration form.

    app.post('/register', (req, res) => {
        res.send('Posted');
    });
    

Step 7: Start the Server

Finally, we need to configure our app to start listening on a specific port.

const PORT = 3001;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

This will start the server on http://localhost:3001.

Step 8: Test the Server

To verify that everything is working correctly, run the following command in your terminal:

node source/server.js

If all went well, you should see the message: "Server is running on port 3001". You can then navigate to http://localhost:3001/register in your browser to see the "Got it" message.

Summary

In this tutorial, we've successfully set up an Express server using Node.js, configured routes for handling incoming requests, and used middleware to serve static files and parse request bodies.

Note (2023 Update): Currently, there are better alternatives to MongoJS, such as Mongoose, which provides a more modern and feature-rich way of interacting with MongoDB. If you're starting a new project, consider switching to Mongoose for better support and schema management.

Video Transcript

00:00Welcome back to the last section in this title
00:03where we're gonna be connecting all the dots from everything
00:06that we've learned so far.
00:08So what we're gonna do, we're gonna be setting up a server,
00:10we're gonna be, well, we're gonna learn new stuff
00:12as we learn how to work with templating systems, namely EJS.
00:17We're gonna run React on the server side,
00:20learning another new thing, taking React
00:21and having it work as a server side component instead
00:25of a client side component.
00:26We're gonna also configure Babel six,
00:28but this time around we're not gonna use Webpac.
00:31And instead of that I'll show you how
00:32to set it up correctly on your own in a really powerful way.
00:37Last but not least, we're gonna fully integrate a full
00:40registration flow using MongoDB.
00:43So by the end of this section,
00:44you're really gonna feel comfortable in
00:46that world of full stack.
00:48So let's jump right into the first lecture
00:50where in our first lecture we're gonna be setting up our
00:52server, and this is gonna be quite familiar
00:55as we set up express, we're gonna use body parser
00:58and we're gonna be connecting routes.
01:00So let's jump right into it.
01:04We've been through a lot together,
01:06but we're gonna go through a lot more in this section.
01:09Um, I wanted to start off completely from scratch just so
01:13you connect all the dots
01:15because we learned a lot of things in the last few sections.
01:19So let's jump right into it
01:20and you'll find in your folder structure,
01:23I would say just create a completely new folder
01:26and let's start from scratch together.
01:28Alrighty, so let's go into our terminal.
01:30And in the terminal. The first thing I want to do,
01:32and I'm inside of that React folder, what I wanna do in the,
01:36in my case, folder number six, what I want
01:38to do is I wanna start up NPM.
01:40So I'm going to call that in it function to start up our,
01:43um, shell shell.
01:45So we'll have something inside.
01:47This is not really our, our output shell
01:49that we're gonna have really,
01:50but we're gonna start from this.
01:52So we have our basic starting point.
01:54Now in the folder itself, I also want to create a new folder
01:57and I'm gonna call it public.
01:58And there we're gonna put all of our public files
02:01and I'm just gonna go ahead into our earlier React folder
02:05and I'm just gonna grab the CSS folder
02:07and just put it right into that folder into the public area.
02:11And that's really the only public
02:12thing that we're gonna be using.
02:14Our CSS is, okay, beautiful.
02:16Now that we have the basic files in place,
02:18let's start building things out.
02:21So I'm gonna go back into my terminal. And in my terminal.
02:23What I wanna do next is I want to install a few things
02:26that you already know of.
02:27So I'm gonna go ahead and install
02:29and save it into our production environment.
02:33I want to install a server, the server
02:35framework itself express.
02:37I want to install Mongo js
02:39because I want to save some information onto our DB
02:42because we're gonna create a registration page.
02:45I also want to use Body Parer as we store previously.
02:48We used that to, to get all access to our, um,
02:53information that is being sent.
02:55Alright, so I'm gonna go ahead and click Q and enter
02:57and I'm gonna start the installation process.
03:00While things are installing,
03:01I'm just gonna go back into our, well,
03:03I'm gonna go into our editor
03:05and to do that I am going to go into my editor
03:08and I'm gonna create a brand new file.
03:11And that brand new file will be called, let's call it server
03:14js and contrary to what we were calling it so far.
03:17And lemme go ahead here and save it
03:21and I'm gonna call it,
03:23let me just make sure I'm in the right folder.
03:27And folder number six, server save.
03:31Now if already I'm um,
03:33let's prepare things the way we're really gonna get them
03:36because in the near very, very near future in this section,
03:40what I'm gonna do is I'm gonna, I'm gonna want to add, um,
03:44JavaScript version six JJSX as we call it,
03:48into no JS as well.
03:50But no, JS doesn't really completely support it,
03:53at least not in this given time.
03:55And what I really want to do is I wanna make sure that all
03:58of my code is converted into ES five
04:00before the server even starts,
04:02which is something I'm gonna do in a later date.
04:04Now to make that happen, I'm gonna go ahead
04:06and create another folder
04:08and I'm gonna call this folder source.
04:10Now, anything that's sitting inside of the source, such
04:12as our server, is gonna basically be pre-compiled
04:17or pre-built before we actually deploy it out.
04:20Now some people call the folder deploy,
04:22some people call the folder build.
04:25I personally love using um, deploy, but I'm gonna use Build
04:28because it's a more common name.
04:30Um, I'm gonna call it build.
04:31And build for me is gonna be, these are the stuff that are,
04:34the server is actually gonna be using,
04:36well the source is gonna be the stuff that need
04:38to be compiled before the server is really gonna run.
04:41So lemme go ahead here and just close that.
04:43We're not gonna save it and I'm gonna go into my package
04:46to make sure that, um, my main file is not actually indexed,
04:49but instead of that it's gonna be our,
04:52for now it's gonna be our source slash index.
04:55Okay? So I'm just making sure that it's the right file.
04:58Let's go back into our source and I'm just gonna open it up
05:01and let's go ahead and start building
05:02things out now that we have.
05:04And let me just double check that everything was installed,
05:06not that it matters 'cause we're gonna have a bit of time
05:08before, uh, we, we really have to get through this.
05:12So we're good and let's continue.
05:14Okay, so first of all, let's build out
05:16our constants that we're gonna need.
05:19We're gonna need express
05:21and our express, let's just load it up
05:27and lemme just go ahead here just to speed things up.
05:29I'm gonna go ahead and just copy this
05:32and we're gonna go ahead
05:33and load in all the things that are critical for us.
05:37So first of all, let me just go ahead here.
05:39I'll set our app and our app.
05:42I'm just gonna ask for express itself.
05:44I calling Express and invoking it.
05:48And the next thing that I'm gonna wanna load is the Mongo js
05:53and we're gonna ask for Mongo js. And
05:56Last but not least for now, we're gonna load up also body
05:59parser and we're gonna ask for the Body
06:03par sir package.
06:06And there we go. So we loaded up our contents,
06:08let's go ahead and also complete building up our server.
06:11So as as as we just created, we created a public folder
06:15inside of our folder structure.
06:17And what I wanna do is I wanna make sure whatever's inside
06:20of the public folder is just web accessible.
06:22To do that, I'm just gonna go ahead
06:24and I'm gonna ask from Express to use
06:29the express do static
06:34and we're gonna tell it that the public folder is
06:36that static zone, basically telling it
06:38that if it's in the public folder, good.
06:40Just serve it up. Alright, next.
06:42We also wanna make sure that when we're gonna do our posts,
06:45we wanna make sure that our body is being sent.
06:48So I'm gonna go ahead and use that, our body parser
06:54and I'm gonna ask for the URL encoded
06:58and extended just like we've done in previous lectures.
07:02And what this is gonna do is gonna make sure that
07:04whenever there is a post we'll be able
07:06to access the things that are being post.
07:08So let's go ahead and just create a route, one route
07:11for our new server that we're creating
07:13and we're gonna focus on register.
07:15Now for our register, we want to have a get,
07:18it's gonna have, we're going use a, a triangle function.
07:24I function actually Why triangle? It sounds very similar.
07:27Okay, post, let me just make sure that,
07:30lemme just copy the whole thing here as well.
07:32So we have a, we have your get,
07:34we have your post for our get.
07:36I'm just gonna, literally,
07:37all I'm gonna do right now is I'm gonna respond, send.
07:41Got it. Okay.
07:42And for now, although we won't have access to the post
07:44because we, 'cause we didn't build it out yet.
07:46Let me just go ahead there
07:48and I'm gonna set it to be posted.
07:51Beautiful. Now we have only one more thing that we have
07:54to do to really get our server to run is we want to go
07:56to our app and I'm gonna call Listen
07:58because we want to actually have our server run
08:01and I'm gonna use Port 3001.
08:04I'm gonna create an ar another arrow function
08:06and I'm gonna go ahead and create your console call.
08:11And I'm gonna say our server is running on port
08:153001.
08:17There we go. We've just created the basics that we need
08:20to get our server running.
08:21Let's go, just go ahead and test if it actually works.
08:24And in our terminal, I want to go ahead
08:26and I'm gonna type, um, what am I gonna tell?
08:29Node and our file inside of our source. And it's the index.
08:35Is it index? No, we call it server.
08:37So lemme go ahead and run it. And let's see.
08:39So first of all, we have your little error.
08:40Let's see what's our error? It's saying that it's not
08:44invalid left hand side in assignment.
08:46Oh, 'cause we see, I, I forgot to close my arrow,
08:49so lemme close that arrow down.
08:51You see that's what happens when you call it
08:52a triangle instead of an arrow. And
08:54Lemme clear and let's run it one more time
08:57and our server is running.
08:58And if I go into our server
09:00and I just go into our register, we should get there.
09:04Got it. There we go.
09:05So we set up our, our structure,
09:07our basic structure for our server.
09:13In this lecture we set up our express using routes
09:16and we've used body parser In the next lecture,
09:19we're gonna continue down the path as we learn about
09:22template systems, namely
09:24and more explicitly how to use EJSI.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!