Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ExpressWorks

This repository hosts my solutions for the NodeSchool's workshop ExpressWorks

Solutions

  1. Hello World

Create an Express.js app that outputs "Hello World!" when somebody goes to /home.

The port number will be provided to you by expressworks as the first argument of the application, i.e., process.argv[2].

Solution

  1. Static

This exercise is about serving static assets like HTML files. There are many ways to do it, but we want you to apply static middleware to serve the file index.html.

Please don't use ANY routes like app.get. ONLY static.

Your solution must listen on the port number supplied by process.argv[2].

The index.html file is provided and usable via the path supplied by process.argv[3]. However, you can use your own file with this content (beware of whitespace):

<html>
  <head>
    <title>expressworks</title>
    <link rel="stylesheet" type="text/css" href="/main.css"/>
  </head>
  <body>
    <p>I am red!</p>
  </body>
</html>

Solution

  1. Pug

Create an Express.js app with a home page rendered by the Pug template engine.

The home page should respond to /home.

The view should show the current date using new Date.toDateString().

We use toDateString() to simply return the date in a human-readable format without the time.

Solution

  1. Good Old Form

Forms are important. This exercise will teach you how to process the traditional (non-AJAX) web form.

Write a route ('/form') that processes HTML form input (<form><input name="str"/></form>) and prints the value of str backwards.

To handle a POST request, use the post() method which is used the same way as get():

```
app.post('/path', function(req, res){...})
```

Express.js uses middleware to provide extra functionality to your web server.

Simply put, a middleware is a function invoked by Express.js before your own request handler.

Middleware provide a large variety of functionality such as logging, serving static files, and error handling.

A middleware is added by calling use() on the application and passing the middleware as a parameter.

To parse x-www-form-urlencoded request bodies, Express.js can use urlencoded() middleware from the body-parser module.

```
var bodyparser = require('body-parser')
app.use(bodyparser.urlencoded({extended: false}))

[Solution](good-old-form/)

5. ### Stylish CSS
HTML without styles is boring so this exercise will teach you how to use Stylus with Express on the fly.

Style the HTML from the "STATIC" exercise using Stylus middleware.
Stylus <https://github.com/stylus/stylus> generates .css files on-the-fly from .styl files.

Your solution should listen on the port supplied by process.argv[2] for
GET requests, one of which will be for main.css, which should be
automatically generated by your Stylus middleware. index.html and main.styl can be found in process.argv[3] (they are in the same directory).

You could also create your own folder and use these, if you like:

The main.styl file:

  ```
  p
    color red
  ```

The index.html file:

  ```
  <html>
    <head>
      <title>expressworks</title>
      <link rel="stylesheet" type="text/css" href="/main.css"/>
    </head>
    <body>
      <p>I am red!</p>
    </body>
  </html>
  ```
[Solution](stylish-css/)

6. ### Param Pam Pam

This exercise is about using URL parameters.
For example, if you have `/message/526aa677a8ceb64569c9d4fb`, then you should know how to extract that value which is an ID of the message.

Create an Express.js server that processes `PUT /message/:id` requests
and produces a SHA-1 hash of the current date combined with the ID from the URL.

For instance, if the server receives

  `PUT /message/526aa677a8ceb64569c9d4fb`

it will respond with a hash of the current date (as a string) and the ID.

The SHA-1 can be computed like this:

require('crypto') .createHash('sha1') .update(new Date().toDateString() + id) .digest('hex')


[Solution](param-pam-pam/)

7. ### What's in Jquery

Oftentimes, we need to process the data from the URL query string (urlencoded).

Write a route that extracts data from the query string in the GET /search URL route, e.g. `?results=recent&include_tabs=true` and then outputs it back to
the user in JSON format.

Use `app.get('/search', function(){...})` for the route.

In Express.js, to extract query string parameters, we can use (inside the request handler):

`req.query.NAME`

[Solution](whats-in-jquery/)

8. ### JSON me!

Most of the times we're building RESTful API servers with JSON.

Write a server that, when it receives a `GET`, reads a file, parses it to JSON,
and responds with that content to the user.

The server should respond to any GET that matches the /books resource path.
As always, the port is passed in `process.argv[2]`. The file to read is passed
in `process.argv[3]`.

Respond with:

  ```
  res.json(object)
  ```

Everything should match the `/books` resource path.

For reading the file, use the `fs` module, e.g.,

fs.readFile(filename, callback)


[Solution](json-me/)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages