2017-10-15 11:31:44 +08:00
const express = require ( 'express' ) ;
const path = require ( 'path' ) ;
const favicon = require ( 'serve-favicon' ) ;
const logger = require ( 'morgan' ) ;
const cookieParser = require ( 'cookie-parser' ) ;
const bodyParser = require ( 'body-parser' ) ;
2017-10-16 04:32:49 +08:00
const helmet = require ( 'helmet' ) ;
const session = require ( 'express-session' ) ;
2017-10-16 05:07:34 +08:00
const FileStore = require ( 'session-file-store' ) ( session ) ;
2017-10-15 11:31:44 +08:00
2017-10-16 07:10:36 +08:00
const indexRoute = require ( './routes/index' ) ;
2017-10-16 04:32:49 +08:00
const loginRoute = require ( './routes/login' ) ;
const logoutRoute = require ( './routes/logout' ) ;
const migrationRoute = require ( './routes/migration' ) ;
// API routes
2017-10-16 05:31:49 +08:00
const treeApiRoute = require ( './routes/api/tree' ) ;
const notesApiRoute = require ( './routes/api/notes' ) ;
const notesMoveApiRoute = require ( './routes/api/notes_move' ) ;
const auditApiRoute = require ( './routes/api/audit' ) ;
const noteHistoryApiRoute = require ( './routes/api/note_history' ) ;
const recentChangesApiRoute = require ( './routes/api/recent_changes' ) ;
const settingsApiRoute = require ( './routes/api/settings' ) ;
const passwordApiRoute = require ( './routes/api/password' ) ;
const migrationApiRoute = require ( './routes/api/migration' ) ;
2017-10-15 11:31:44 +08:00
const db = require ( 'sqlite' ) ;
2017-10-16 07:47:05 +08:00
const config = require ( './services/config' ) ;
2017-10-15 11:31:44 +08:00
db . open ( config . Document . documentPath , { Promise } ) ;
const app = express ( ) ;
// view engine setup
app . set ( 'views' , path . join ( _ _dirname , 'views' ) ) ;
app . set ( 'view engine' , 'ejs' ) ;
2017-10-16 04:32:49 +08:00
app . use ( helmet ( ) ) ;
2017-10-15 11:31:44 +08:00
app . use ( logger ( 'dev' ) ) ;
app . use ( bodyParser . json ( ) ) ;
app . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
app . use ( cookieParser ( ) ) ;
2017-10-16 07:47:05 +08:00
app . use ( express . static ( path . join ( _ _dirname , 'public' ) ) ) ;
2017-10-16 04:32:49 +08:00
app . use ( session ( {
secret : "sdhkjhdsklajf" , // FIXME: need to use the DB one
resave : false , // true forces the session to be saved back to the session store, even if the session was never modified during the request.
saveUninitialized : false , // true forces a session that is "uninitialized" to be saved to the store. A session is uninitialized when it is new but not modified.
cookie : {
// path: "/",
httpOnly : true ,
maxAge : 1800000
2017-10-16 05:07:34 +08:00
} ,
store : new FileStore ( {
2017-10-17 07:14:15 +08:00
ttl : 30 * 24 * 3600 ,
2017-10-16 07:47:05 +08:00
path : '../trilium-data/sessions'
2017-10-16 05:07:34 +08:00
} )
2017-10-16 04:32:49 +08:00
} ) ) ;
2017-10-21 12:19:13 +08:00
app . use ( favicon ( _ _dirname + '/public/images/app-icons/favicon.ico' ) ) ;
2017-10-15 11:31:44 +08:00
2017-10-16 07:10:36 +08:00
app . use ( '/' , indexRoute ) ;
2017-10-16 04:32:49 +08:00
app . use ( '/login' , loginRoute ) ;
app . use ( '/logout' , logoutRoute ) ;
app . use ( '/migration' , migrationRoute ) ;
2017-10-16 05:31:49 +08:00
app . use ( '/api/tree' , treeApiRoute ) ;
app . use ( '/api/notes' , notesApiRoute ) ;
app . use ( '/api/notes' , notesMoveApiRoute ) ;
app . use ( '/api/audit' , auditApiRoute ) ;
app . use ( '/api/notes-history' , noteHistoryApiRoute ) ;
app . use ( '/api/recent-changes' , recentChangesApiRoute ) ;
app . use ( '/api/settings' , settingsApiRoute ) ;
app . use ( '/api/password' , passwordApiRoute ) ;
app . use ( '/api/migration' , migrationApiRoute ) ;
2017-10-15 11:31:44 +08:00
// catch 404 and forward to error handler
app . use ( function ( req , res , next ) {
const err = new Error ( 'Not Found' ) ;
err . status = 404 ;
next ( err ) ;
} ) ;
// error handler
app . use ( function ( err , req , res , next ) {
// set locals, only providing error in development
res . locals . message = err . message ;
res . locals . error = req . app . get ( 'env' ) === 'development' ? err : { } ;
// render the error page
res . status ( err . status || 500 ) ;
res . render ( 'error' ) ;
} ) ;
2017-10-22 09:10:33 +08:00
require ( './services/sync' ) ;
2017-10-15 11:31:44 +08:00
module . exports = app ;