Skip to content

svvald/nodejs-course

Repository files navigation

EPAM Node.js Global Mentoring Program

Prerequisites

Install all reqiured dependencies by executing the following command:

npm install

Homework 1

Task 1

Write a program which reads a string from the standard input stdin, reverses it and then writes it to the standard output stdout.

Launch and using notes

Run the following command to start a program:

npm run hw1-task1

To finish executing use Ctrl+C (Control+C on Mac).

Task 2

Write a program which should do the following:

Launch and using notes

The csv file is stored inside homework-1/csv folder. You can replace by your own if you want to.

Run the following command to start a program:

npm run hw1-task2

The resulted txt file will be placed in homework-1 folder named as data.txt.

Homework 2

Write a simple REST service with CRUD operations for User entity.

  • To create REST service, use ExpressJS (https://expressjs.com/).
  • The User should have the following properties:
    • id: string
    • login: string
    • password: string
    • age: number
    • isDeleted: boolean
  • Service should have the following CRUD operations for User:
    • get user by id;
    • create and update user;
    • get auto-suggest list from limit users, sorted by login property and filtered by loginSubstringin the login property: getAutoSuggestUsers(loginSubstring, limit);
    • remove user (soft delete – user gets marked with isDeleted flag, but not removed from the collection).
  • Store user’s collection in the service memory (while the service is running).

Launch and using notes

Run the following command to start a program:

npm run hw2

Use the following endpoints to interact with REST-service:

  • [GET] /users/:id - get user by id
  • [GET] /users?loginSubstring={""}&limit={10} - get auto-suggest list of users
  • [POST] /users - create a user
  • [PUT] /users/:id - update user
  • [DELETE] /users/:id - mark user as deleted

The body required for create and update requests:

{ login: string, password: string, age: number }

Homework 3

Install DB PostgreSQL on your machine or use a free web hosting services for PostgreSQL (https://www.heroku.com/postgresor https://www.elephantsql.com/plans.html).

  • Write SQL script which will create Users table in the DB and fill it in with predefined users’ collection.
  • Configure your REST service to work with PostgreSQL.
  • Use the sequelize package(http://docs.sequelizejs.com/) as ORM to work with PostgreSQL.

The service should adhere to 3-layer architecture principles (https://softwareontheroad.com/ideal-nodejs-project-structure/) and contain the following set of directories:

  • routers / controllers
  • services
  • data-access
  • models

Homework 4

Add Group entity to already existing REST service with CRUD operations.

  • TheGroup entity should have the following properties:
    • id: string
    • name: string
    • permissions: Array<Permissions>

type Permissions = 'READ' | 'WRITE' | 'DELETE' | 'SHARE' | 'UPLOAD_FILES'

  • The service should provide the following CRUD operations for Group:
    • get group by id;
    • get all groups;
    • create and update a group;
    • remove group (hard delete – group data is fully removed from the DB).
  • Storing of groups data should be done in PostgreSQL in Groups table.
  • The service should follow the principles of 3-layer architecture.

Link User records in one table with Group records in another table.

  • Add a UserGroup table("many-to-many" relationship) which will store the data describingwhich users are assigned to which group.
  • If any record gets removed from the DB, then all linked records should be removed from UserGroup as well.

Add addUsersToGroup(groupId, userIds) method which will allow adding users to a certain group. Use transactions to save records in DB.

Launch and using notes

Prior to launch the service itself one needs to have PostgreSQL instances installed and running locally or in cloud. Then one should create a .env file in the root folder containing the following properties:

  • APP_PORT (optional)
  • DB_HOST
  • DB_PORE
  • DB_NAME
  • DB_USER
  • DB_PASSWORD

The next step is to fill the DB with the predefined data. This can be done by running the following command:

npm run db:seed:

If one needs to have DB cleared, this can be achieved by running the following command:

npm run db:clear

Finally, the service could be started by running the executing command:

npm run hw3

In addition to the endpoints listed above, use the following endpoints to interact with REST-service:

  • [GET] /groups - get all groups
  • [GET] /groups/:id - get group by id
  • [POST] /group - create a group
  • [PUT] /groups/:id - update group
  • [DELETE] /groups/:id - delete group
  • [POST] /groups/:id/addUsers - add users to group

The body required for create and update requests:

{ name: string, permissions: Array<Permission> }

The body required for addUsers request:

{ userIds: Array<string> }

Homework 5

Add express middleware which will log which service method has been invoked and which arguments have been passed to it.

Add express middleware which will log all unhandled errors and return a standard message with HTTP code 500 (Internal Server Error).

Add error handling to process.on(‘uncaughtException’,...).

Add Unhandled promise rejection listener to log errors.

Every method in the controllers should log the errors which should include the following information:

  • method name
  • arguments which have been passed to the method
  • error message

Add middleware wrapper functions (or decorators) which will track the methods’ execution time

Homework 6

Add authorization to the already existing REST service:

  • Add login(username, password) method which should return JWT token.
  • Add a middleware which will proxy all the requests (except login) and check that HTTP Authorization header has the correct value of JWT token.
  • In case of the HTTP Authorization header is absent in the request, the middleware should stop further controller method execution and return HTTP 401 code (Unauthorized Error) and standard error message.
  • In case of HTTP Authorization header has invalid JWTtoken in the request, the middleware should return HTTP code 403 (Forbidden Error) and standard error message.

Add CORS middleware to access service methods from WEB applications hosted on another domains (https://github.com/expressjs/cors).

JWT secret

An additional entry JWT_SECRET should be added to the .env file prior to launching the service. Otherwise, a random number converted to string will be used as a JWT secret.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published