Skip to content

iliatcymbal/base-koa

Repository files navigation

base-koa

Installation

  1. Clone repo git clone https://github.com/iliatcymbal/base-koa.git
  2. Go to project folder and make npm i
  3. Run npm start to start server. Server is available on http://localhost:8086/
  4. Check URL http://localhost:8086 you should see greeting text (aka 'Hello, user')
  5. In case you need to change port number go to app/index.js and find line with code const PORT = 8086;. Replace 8086 with required number

Static content

Put all your html/js/css/images to static folder to check how your bundle works on server

API

All urls have public and private mode. Public urls start with public/ prefix and do not require any authenticating credentials.
Private urls require special cookie (ECSID) in every request. User gets this cookie after successful login.
Do not forget to use withCredentials flag for CORS stuff (see details).
If you are using ES6 fetch() method do not forget settings credentials: 'include' (using fetch)

Specific endpoints

You can find description for tasks and for categories

User API

public/login post { email, password } - by default there is one user in the system with admin@a.com/admin credentials. On success returns object with user fields {...} On error returns 401 error 'Password or email wrong'

There is an example for login request with pure js:

  const xhr = new XMLHttpRequest();
  xhr.open('POST', 'http://localhost:8086/public/login');
  xhr.onload = () => {
    if (xhr.status === 200) {
      console.log(xhr.responseText);
    } else {
      throw new Error(xhr.responseText);
    }
  };
  xhr.withCredentials = true;
  xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
  xhr.send(JSON.stringify({ email: 'admin@a.com', password: 'admin' }));

Using es6 fetch method:

fetch('http://localhost:8086/public/login', {
    method: 'POST',
    credentials: 'include',
    headers:{
      'Content-type': 'application/json; charset=utf-8'
    },
    body: JSON.stringify({ email: 'admin@a.com', password: 'admin' })
  })

AJAX example with jquery:

$.ajax({
  type: 'post',
  url: 'http://localhost:8086/public/login',
  data: { email: 'admin@a.com', password: 'admin' },
  xhrFields: {
    withCredentials: true
  }
});

public/user post { firstname, lastname, email, password } - create new user

user put { firstname, lastname, password } - update existing user

public/checkUser get - if user is authenticated, return object with user {...}, in other case - 404 error { error: "User is not authenticated"}

logout get - sends to server request to logout user

If you need to add new route check new route.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published