Coder Social home page Coder Social logo

node-express-server-intro's Introduction

NodeJs and Express Tutorial

Hello

Description

This is a reference for the NodeJS and Express intro tutorial. Using this, you can create a basic JSON server. If you get stuck in the tutorial, you can use this as a reference.

Setup

  1. Download or clone this repo
  2. Navigate to the root directory
  3. Install Depandancies: npm install
  4. Start the server file: node server.js

node-express-server-intro's People

Contributors

grobergm avatar m3kh avatar

Watchers

 avatar  avatar

node-express-server-intro's Issues

Prepare your environment

Traditionally, JavaScript is a browser-side programming language. However, Node.js gives you the ability to run JavaScript files on the server-side. Express is a library for Node.js, that allows you to make requests to different "endpoints" and get a response back.

In this tutorial you will learn how to:

  1. Set up a project using Node.js and NPM
  2. Send JSON data using Express
  3. Test your API using PostMan
  4. Secure your routes with middleware

When you see the ๐Ÿ’ก symbol, the following info is a optional tip, or context.

This tutorial is interactive. Look for instructions on how to complete each step at the bottom of the issue. If the next step doesn't appear, try refreshing the page.

When you are ready to start, close this issue.

Start express server

Alright, now let's start making a server! Open up your server.js file and add these two lines:

const express = require('express');
const app = express();

The first line gives you access to the express library by searching your node_modules for "express". The next creates an instance of the express constructor, which we will name "app".
We can now access methods used for making a server by including their name after app. Add this to the bottom of server.js:

app.listen(8000,function(){
console.log("server is running")
})

The app.listen method will start up the server locally on the port you give as its first argument (in this case the base url is: http://localhost:8000)

But first we need to run the server.js file by entering this in the terminal: node server.js

If everything was successful, you should see the console.log message we supplied in the callback: "server is running". This happens because the file is being run on our terminal. To end this process, push CTRL+C. Whenever you make changes to your server, you need exit and restart it.

Once your server is working, push your changes up to GitHub to complete this step.

git add server.js
git commit -m"set up express server"
git push origin master

Set up Node.js environment

Installation

First, let's install Node.js, and an app called Postman to test our API. You will be coding along with me, so make sure you also have git installed.

Node.js comes with something called NPM (Node Package Manager) that we will use manage our node modules. You can check if you have Node, NPM, and Git installed by running these commands (one at a time):

node -v
npm -v
git --version

You should get a version back from each of these commands. If you get an error, you'll want to check that it was correctly installed.

Clone Template Repository

By signing up for this course, a template repository was created on your GitHub account. Clone this repository locally on your computer and navigate to it by running:

git clone https://github.com/M3kH/node-express-server-intro.git
cd node-express-server-intro

File Setup

Open the folder you just cloned in your favorite text editor.
You should have a few files already created:

middleware.js
.gitignore
server.js

The server file will be the main one we use, and middleware will be used near the end. In the .gitignore folder, you should see this line at the top /node_modules

Express (the server library we will install) is a node_module. Rather than track the entire library in our git history, we will create a package.json file, which will allow us to install and save node modules.

Run this from your command line: npm init -y

You should have a package.json file in your folder now.

Now you can install Express using NPM. Enter this command in the terminal: npm install express --save

A folder called node_modules should apear in your project. This is where the actual files for express are stored. If you open the folder, you can see how many files there are. These will only be stored on your machine, because we told git to ignore this entire directory.

In your package.json file and make sure you have something like this under your dependencies:

"dependencies": {
    "express": "^4.17.1",
  }

The number on the right is the version you downloaded. Since we aren't tracking the actual node_modules folder, this section is a reference used to re-install the modules your application depends on.

When you are finished with this section, push your file to github for the next step:

git add .
git commit -m "initial file setup"
git push origin master

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.