Frameworks

[Express] Basic concept notes

Middleware (handlers)

  • Order-sensitive
  • Triggered by previous MW next()
  • Every middleware should call next() or end the request-response cycle
  • Changes to req and res will be passed down
  • App level MW: Bind to app
  • Router level MW: Bind to express.Router()
    • next('route'): Bypass remaining route callbacks
    • next('router'): Bypass remaining routes
  • Error-handling MW: (err, req, res, next) => { … }

Error handling

  • When an error occurs with next(myErr), remaining non-error MW will be skipped
  • Synchronous code: throw error;
  • Asynchronous code
    • Express 4: next(error);
    • Express 5: automatically call next(error);

[Django] 常用基本指令

  • 新建專案
django-admin startproject mysite
  • 新建app
python manage.py startapp myapp
  • 啟動sever
python manage.py runserver localhost:443
  • 建立super user
python manage.py createsuperuser

[Django] Manage django database using additional scripts

  • If you just want to use django ORM, please refer to Django ORM Standalone Template.
  • If you want to use other python code to manipulate existing django database inside a project, using its pretty ORM, please do as following code:
# setup django environment using existing settings file
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")
django.setup()

# start of main code
from myapp.models import *

testobjs = mymodel.objects.all()
print(testobjs)