-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (60 loc) · 1.81 KB
/
server.js
File metadata and controls
76 lines (60 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const path = require("path");
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Set view engine
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
// Serve static files
app.use(express.static(path.join(__dirname, "public")));
// Store current markdown content
let currentMarkdown = `# Welcome to Real-time Markdown Editor
## Features
- **Real-time collaboration** - Multiple users can edit simultaneously
- **Live preview** - See your markdown rendered instantly
- **Syntax highlighting** - Beautiful code blocks
### Try editing this content!
You can write:
- Lists like this
- **Bold text**
- *Italic text*
- \`inline code\`
\`\`\`javascript
// Code blocks with syntax highlighting
function hello() {
console.log("Hello, World!");
}
\`\`\`
> Blockquotes for important notes
[Links work too](https://example.com)
`;
// Routes
app.get("/", (req, res) => {
res.render("index", {
title: "Real-time Markdown Viewer",
initialMarkdown: currentMarkdown,
});
});
// Socket.IO connection handling
io.on("connection", (socket) => {
console.log("New user connected:", socket.id);
// Send current markdown to new user
socket.emit("markdown-update", currentMarkdown);
// Handle markdown changes
socket.on("markdown-change", (data) => {
currentMarkdown = data.markdown;
// Broadcast to all other connected users
socket.broadcast.emit("markdown-update", currentMarkdown);
});
// Handle user disconnect
socket.on("disconnect", () => {
console.log("User disconnected:", socket.id);
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});