# News Editor Full-stack application for creating, editing, and publishing news articles with real-time notifications. ## Architecture ``` ┌──────────────────────────┐ │ React Client │ │ Login/Register, News list, │ Markdown editor, Preview, │ Notifications bell └─────────────┬────────────┘ │ REST + JWT / Socket.IO ▼ ┌──────────────────────────┐ │ Express Backend │ │ Auth middleware, News CRUD, │ Upload, Scheduler, │ Socket.IO notifications └─────────────┬────────────┘ │ ▼ ┌──────────────────────────┐ │ MongoDB Atlas │ │ users, news, │ notifications └──────────────────────────┘ ``` ## Tech Stack | Layer | Technology | |----------|-----------------------------------------| | Backend | Node.js, Express, MongoDB/Mongoose | | Auth | JWT (jsonwebtoken), bcrypt | | Real-time| Socket.IO | | Upload | multer (local storage + express.static) | | Frontend | React 18, Vite, function components | | Editor | Markdown (react-markdown, remark-gfm) | | Tests | Vitest, Supertest, mongodb-memory-server| ## Prerequisites - Node.js >= 18 - MongoDB Atlas cluster (or local MongoDB) - npm ## Setup ### 1. Install dependencies ```bash npm install npm --prefix server install npm --prefix client install ``` ### 2. Configure environment ```bash cp server/.env.example server/.env cp client/.env.example client/.env ``` Edit `server/.env`: ```env MONGODB_URI=mongodb+srv://:@.example.net/newsEditor JWT_SECRET= CLIENT_ORIGIN=http://localhost:5173 ``` Edit `client/.env`: ```env VITE_API_URL=http://localhost:3000 ``` ### 3. Run ```bash # Both server + client (uses concurrently) npm run dev # Or separately npm --prefix server run dev npm --prefix client run dev ``` - Backend: http://localhost:3000 - Frontend: http://localhost:5173 - Health check: http://localhost:3000/api/health ## API Endpoints ### Auth | Method | Path | Auth | Description | |--------|----------------------|------|--------------------------------| | POST | `/api/auth/register` | – | Register, returns `{user, token}` | | POST | `/api/auth/login` | – | Login, returns `{user, token}` | ### News (authenticated) | Method | Path | Description | |--------|------------------------|------------------------------------| | GET | `/api/news` | List own news (`?status=&page=&limit=`) | | GET | `/api/news/:id` | Get one news article | | POST | `/api/news` | Create draft | | PATCH | `/api/news/:id` | Update own article | | DELETE | `/api/news/:id` | Delete own article | | POST | `/api/news/:id/publish`| Publish immediately | | POST | `/api/news/:id/schedule`| Schedule publication (`{publishAt}`) | ### Public | Method | Path | Description | |--------|-----------------------|--------------------------------------| | GET | `/api/public/news` | List published articles | | GET | `/api/public/news/:id`| Get one published article | ### Upload | Method | Path | Auth | Description | |--------|---------------|------|------------------------------------------| | POST | `/api/uploads`| ✓ | Upload file (multipart), returns `{file}` | ### Notifications | Method | Path | Auth | Description | |--------|---------------------------------|------|--------------------------| | GET | `/api/notifications` | ✓ | List notifications | | PATCH | `/api/notifications/:id/read` | ✓ | Mark as read | | POST | `/api/notifications/read-all` | ✓ | Mark all as read | ## Scheduled Publication The backend runs a poller every 15 seconds that finds articles with: ```js { status: "scheduled", publishAt: { $lte: now } } ``` and atomically transitions them to `published`. This survives process restarts — no in-memory `setTimeout` per article. ## Real-time Notifications Socket.IO runs on the same HTTP server as Express. The client authenticates with the same JWT via `io(API_URL, { auth: { token } })`. Events: `news.created`, `news.updated`, `news.deleted`, `news.scheduled`, `news.published`. Notifications are persisted to MongoDB so the bell survives page reloads. ## Testing ```bash npm --prefix server test # Vitest + Supertest + mongodb-memory-server npm test # all tests ``` ## Lint ```bash npm run lint # ESLint for server + client ``` ## Production Build ```bash npm --prefix client run build # Output: client/dist/ (static files, deployable to any static host) ``` Backend should be deployed to a long-running Node host (not serverless) because of Socket.IO connections and the scheduler. ## Demo Flow 1. Register → Login → see JWT with user `id` 2. Create a draft → add markdown (heading, bold, quote, code block) 3. Upload an image → insert into article 4. Upload a PDF → insert link 5. Open Preview → see rendered markdown 6. Save draft → see notification in bell 7. Schedule publication for 1–2 min ahead → watch it auto-publish 8. Create another article → publish immediately → delete it 9. Bell shows all events in real-time