jarrett huang's web experiments
A diff tool similar to git.
left
diff
right
import express from 'express';
import fs from 'fs';
import path from 'path';
const app = express();
const port = 8080;
const port = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Load user profile
function loadProfile(id) {
const raw = fs.readFileSync(`profiles/${id}.json`, 'utf8');
const file = path.join('profiles', `${id}.json`);
const raw = fs.readFileSync(file, 'utf8');
return JSON.parse(raw);
}
}
// Validate user ID
function validate(id) {
return /^[0-9]+$/.test(id);
}
// Main handler
app.get('/user/:id', (req, res) => {
const id = req.params.id;
if (!validate(id)) {
res.status(400).json({ error: 'invalid id' });
return;
}
const profile = loadProfile(id);
res.json(profile);
});
app.listen(port);
console.log('server started');
console.log('server started on new port');
import express from 'express';
import fs from 'fs';
+
import path from 'path';
const app = express();
-
const port = 8080;
+
const port = 3000;
app.use(express.json());
+
app.use(express.urlencoded({ extended: true }));
// Load user profile
function loadProfile(id) {
-
const raw = fs.readFileSync(`profiles/${id}.json`, 'utf8');
+
const file = path.join('profiles', `${id}.json`);
+
const raw = fs.readFileSync(file, 'utf8');
return JSON.parse(raw);
-
}
-
+
}
+
+
// Validate user ID
+
function validate(id) {
+
return /^[0-9]+$/.test(id);
+
}
+
// Main handler
app.get('/user/:id', (req, res) => {
const id = req.params.id;
+
if (!validate(id)) {
+
res.status(400).json({ error: 'invalid id' });
+
return;
+
}
const profile = loadProfile(id);
res.json(profile);
});
app.listen(port);
-
console.log('server started');
+
console.log('server started on new port');
import express from 'express';
import fs from 'fs';
import path from 'path';
const app = express();
const port = 8080;
const port = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Load user profile
function loadProfile(id) {
const raw = fs.readFileSync(`profiles/${id}.json`, 'utf8');
const file = path.join('profiles', `${id}.json`);
const raw = fs.readFileSync(file, 'utf8');
return JSON.parse(raw);
}
}
// Validate user ID
function validate(id) {
return /^[0-9]+$/.test(id);
}
// Main handler
app.get('/user/:id', (req, res) => {
const id = req.params.id;
if (!validate(id)) {
res.status(400).json({ error: 'invalid id' });
return;
}
const profile = loadProfile(id);
res.json(profile);
});
app.listen(port);
console.log('server started');
console.log('server started on new port');