Store and publish annotations on the web, as described in the Web Annotation Discovery proposal.

web-annotation-discovery-se.../ app.ts
50 lines
1.3 KiB

  1. import express from 'express';
  2. import createError from 'http-errors';
  3. import annotationsRouter from './routes/router.js';
  4. import { create } from 'express-handlebars';
  5. import { annotationHbsParams } from './routes/render/renderAnnotation.js';
  6. import { fileURLToPath } from 'url';
  7. var app = express();
  8. // view engine setup
  9. app.set('views', fullPath('views'));
  10. const hbs = create({
  11. extname: 'hbs',
  12. helpers: {
  13. annotationHbsParams(arg) {
  14. return annotationHbsParams(arg, false);
  15. },
  16. },
  17. });
  18. app.engine('hbs', hbs.engine);
  19. app.set('view engine', 'hbs');
  20. app.use(express.json({ type: ['application/json', 'application/ld+json'] }));
  21. app.use(express.urlencoded({ extended: false }));
  22. app.use(express.static(fullPath('public')));
  23. app.use('/', annotationsRouter);
  24. // catch 404 and forward to error handler
  25. app.use(function (req, res, next) {
  26. next(createError(404));
  27. });
  28. // error handler
  29. app.use(function (err, req, res, next) {
  30. // set locals, only providing error in development
  31. res.locals.message = err.message;
  32. res.locals.error = req.app.get('env') === 'development' ? err : {};
  33. // render the error page
  34. res.status(err.status || 500);
  35. res.render('error');
  36. });
  37. export default app;
  38. function fullPath(filename: string): string {
  39. return fileURLToPath(new URL(filename, import.meta.url));
  40. }