Giter Club home page Giter Club logo

baby-express's Introduction

NodeJS with Express

Create a folder exercises_express then go to this folder in your terminal and execute:

npm init -y

Exercise 1 (Help)

Install express into your directory with the npm install express command. In order to open a server on port 3000, create an server.js file and add this content:

import express from 'express';

const app = express();
app.listen(3000, () => console.log('Server running on port 3000'));

You can now open http://localhost:3000 in the browser. If you see an error, that is expected behaviour as we haven't defined any get route yet. Still, it means your server is up and running.

Exercise 2 (Help)

In this exercise, you are going to create an index route with the get method. In server.js, create the get index route and have it output the string 'How are you?'.

Exercise 3 (Help)

In this exercise, you are going to create the homepage for your app and serve it on the home route. Create a new directory public and inside it, create a html file called homepage.html and write this content in it:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hello</title>
</head>
<body>
  <h1>How are you?</h1>
</body>
</html>

In server.js, get express to send this file when hitting the route home (at http://localhost:3000/home)

Exercise 4 (Help)

In this exercise you'll get your server to send back some JSON when a PUT request is performed. The JSON will look like this:

{"good" : "yep"}

In server.js, get express to send this json when hitting the route home with the method PUT. Try out your request with postman or curl.

Exercise 5 (Help && Help && Help)

Create a directory views under the root directory of your project. Add a file with this template in:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <% if (title) { %>
    <h1><%= title %></h1>
  <% } %>
</body>
</html>

Render this EJS code if the user accesses the URL /test-ejs and render this page with Hey as value of title variable.

Remember that in server.js, you need to define the directory of your views and the name of the template engine you are using this way:

app.set('views', './views');
app.set('view engine', 'ejs');

Keep these lines before the routes for better organisation.

Exercise 6 (Help)

We pass an array to our new page /test-ejs2:

{users : ['Bob', 'John', 'Jane' ]}

Create an EJS page that uses the forEach method to list each element.

Exercise 7 (Help)

Create a template with two input:text fields to enter the first and last name and show it on the /test-ejs3 route.

Exercise 8 (Help)

Submit the first and last name with the previous form to /test-ejs3 POST route.

Display in the console (terminal) the result of the POST as a json with fields name and surname.

Exercise 9 (Help)

Create a route of the type /number/1 where the number will be a variable :id and will be displayed on the page. E.g. on the route /number/1337 we will see:

The number is 1337

Exercise 10 (Help)

If you are tired of always restarting the server, nodemon can come to the rescue. Install it like this npm i nodemon --save-dev (this will also add a new entry under devDependencies in your package.json). Change the npm start script in the package.json to be nodemon server.js.

Now restart the server; you server will automatically reload on file change. To see the newest changes, you'll still have to reload the browser.

Nodemon is only for development use, that's why it's wise to add another entry to your package.json scripts: "start-prod": "node server.js". This is the command you are going to run when you app is deployed on a remote host.

Your scripts entry in package.json should now look like this:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "nodemon server.js",
  "start-prod": "node server.js"
}

baby-express's People

Contributors

feychou avatar

Watchers

 avatar

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.