-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (54 loc) · 2.22 KB
/
index.js
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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const minimist = require('minimist');
const app = express();
// Middleware to parse request bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Parse command-line arguments
const argv = minimist(process.argv.slice(2), {
string: ['addr', 'port', 'output'], // Treat these as strings
alias: { l: 'addr', p: 'port', x: 'output', d: 'output' }, // -p is an alias for --port, -x for --output
default: { addr: 'localhost', port: 3000 } // Default port if none is provided
});
const address = argv.addr;
const port = argv.port;
let basePath = argv.output || ''; // Use provided base path or default to empty string
// Validate the base path
if (!basePath || !fs.existsSync(basePath) || !fs.statSync(basePath).isDirectory()) {
console.error('Error: Invalid base path provided. Please provide a valid directory as a base path.');
process.exit(1);
}
console.log(`Server will listen on ${address}:${port}`);
console.log(`Base folder path set to: ${path.resolve(basePath)}`);
app.use((err, req, res, next) => {
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
console.error('Bad JSON', req.body);
return res.status(400).send('Error: Malformed JSON syntax.');
}
next();
});
// REST API endpoint to check server status
app.get('/status', (req, res) => {
res.json({ status: 'ok', message: 'Server is working' });
});
// REST API endpoint to accept log file path and message
app.post('/log', (req, res) => {
const { filePath, message } = req.body;
const fullPath = path.join(basePath, filePath);
try {
// Ensure the directory exists
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
// Append the message to the log file
fs.appendFileSync(fullPath, message + '\n', 'utf8');
res.json({status: 'ok', message: 'Log message saved successfully.'});
} catch (error) {
console.error('Failed to save log message:', error);
res.status(500).json({status: 'error', message: 'Failed to save log message.'});
}
});
app.listen(port, address, () => {
console.log(`Server listening at http://${address}:${port}`);
});