Implement SimpleNote Web API - full REST API with Express
- Express server with CORS, JSON middleware - Auth middleware (Bearer token) - Document CRUD with markdown storage - Library CRUD with nested support - Tag indexing and search - Error handler middleware - Config from env vars - Init script for data structure
This commit is contained in:
41
src/index.js
41
src/index.js
@@ -4,33 +4,42 @@
|
||||
*/
|
||||
|
||||
import express from 'express';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname, join } from 'path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
import cors from 'cors';
|
||||
import config from './config/index.js';
|
||||
import { createApiRouter } from './routes/index.js';
|
||||
import { errorHandler } from './middleware/errorHandler.js';
|
||||
import { initTagIndexer } from './indexers/tagIndexer.js';
|
||||
import { ensureDir } from './utils/fsHelper.js';
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
// Middleware
|
||||
app.use(express.json());
|
||||
app.use(cors({ origin: config.corsOrigin }));
|
||||
app.use(express.json({ limit: '10mb' }));
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// Health check
|
||||
// Health check (unprotected)
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
||||
res.json({ status: 'ok', timestamp: new Date().toISOString(), version: '1.0.0' });
|
||||
});
|
||||
|
||||
// TODO: Routes
|
||||
// - /api/documents
|
||||
// - /api/libraries
|
||||
// - /api/tags
|
||||
// - /api/auth
|
||||
// Ensure data directory exists
|
||||
ensureDir(config.dataRoot);
|
||||
|
||||
// Initialize tag indexer
|
||||
console.log(`[SimpleNote] Data root: ${config.dataRoot}`);
|
||||
initTagIndexer(config.dataRoot);
|
||||
|
||||
// Mount API routes
|
||||
app.use(config.apiPrefix, createApiRouter(config.apiPrefix));
|
||||
|
||||
// Error handler
|
||||
app.use(errorHandler);
|
||||
|
||||
// Start server
|
||||
app.listen(PORT, () => {
|
||||
console.log(`SimpleNote Web running on port ${PORT}`);
|
||||
app.listen(config.port, config.host, () => {
|
||||
console.log(`[SimpleNote] Web API running on http://${config.host}:${config.port}`);
|
||||
console.log(`[SimpleNote] API prefix: ${config.apiPrefix}`);
|
||||
});
|
||||
|
||||
export default app;
|
||||
|
||||
Reference in New Issue
Block a user