Giter VIP home page Giter VIP logo

node_case-transformer-server's Introduction

Case transformer server

In this task you will learn how to work with http module in Node.js, creating your own simple server.

Requirements

You need to create and export (as an object field) a createServer function inside src/createServer.js. Inside the function you should implement a server (from http module) and return it.

You can create as many files as you want and split logic between them.

Server requirements

Server should have single function - converting text between cases. Supported cases:

  • snake_case (SNAKE)
  • kebab-case (KEBAB)
  • camelCase (CAMEL)
  • PascalCase (PASCAL)
  • UPPER_CASE (UPPER)

Server accepts request to the URL in the next format: /<TEXT_TO_CONVERT>?toCase=<CASE_NAME>.

How to parse URL query params Node.js has built-in global class `URLSearchParams`. [Documentation](https://nodejs.org/api/url.html#class-urlsearchparams). It will do work for you.
How to parse URL hint First, split `req.url` by `?`. The first part is almost the text you need to convert. The second param is a query string. Use `URLSearchParams` to parse it: ```javascript const params = new URLSearchParams(queryString); const toCase = params.get('toCase'); ```

Examples: For request /createServer?toCase=SNAKE result should be create_server.

Business logic (converting cases) are carefully implemented for you to focus on work with the server specific stuff. You need to work on reading data from URL, validating it, forming response and errors.

General rules

Server should always respond with a JSON type. That means you should always add a correct Content-Type header.

Validation

Text in the URL and query param toCase are mandatory. Also, toCase value should be one of the supported cases. If something is not correct, you should respond with 400 status, Bad request statusText and the next payload:

{
  "errors": [
    {
      "message": "<SPECIFIC MESSAGE TEXT HERE>"
    }
  ],
}

Array of messages can contain more than one error. For example, if both text and case are not provided.

Messages:

  • If text is missing: Text to convert is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".
  • If toCase is missing: "toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".
  • If toCase value is not from listed above: This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.

Invoke business logic

If validation is called you should invoke business logic (convertToCase function from src/convertToCase folder). It accepts two params: case name and text to convert.

Business logic is also covered with tests. They are already passed. Just for you to be sure that it works correctly.

Function return the next object:

{
  originalCase: 'CASE_NAME',
  convertedText: 'CONVERTED_TEXT',
}

For example:

const result = convertToCase('UPPER', 'writeFile');

console.log(result); // { originalCase: 'CAMEL', convertedText: 'WRITE_FILE' }

Respond to the client

You should respond with status 200 and OK status text.

Response body should be the next JSON:

{
  "originalCase": "CASE_NAME",
  "targetCase": "CASE_NAME",
  "originalText": "ORIGINAL_TEXT",
  "convertedText": "CONVERTED_TEXT"
}

Example:

{
  "originalCase": "KEBAB",
  "targetCase": "PASCAL",
  "originalText": "hello-world",
  "convertedText": "HelloWorld"
}

Guidelines to work on project

  • Fork this repo.
  • After cloning repo, run npm i.
  • Run npm run test:watch to have automatically rerun tests on code change.
  • Work until all tests are green.
  • Commit and push changes.
  • Make PR to Mate academy repo.

You can run npm start to have working server locally.

node_case-transformer-server's People

Contributors

yuriiholiuk avatar andr1yk 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.