- Clone repo git clone https://github.com/iliatcymbal/base-koa.git
- Go to project folder and make npm i
- Run npm start to start server. Server is available on http://localhost:8086/
- Check URL http://localhost:8086 you should see greeting text (aka 'Hello, user')
- In case you need to change port number go to app/index.js and find line with code
const PORT = 8086;
. Replace8086
with required number
Put all your html/js/css/images to static folder to check how your bundle works on server
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)
You can find description for tasks and for categories
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.