Compare commits

..

2 Commits

2 changed files with 34 additions and 6 deletions

View File

@@ -11,6 +11,10 @@ Wake-on-LAN API in Node.js
2. Edit the file and add your targets
## Usage
Navigate to `http://localhost:9999` and just click the target you want to wake up.
Or:
### POST to `/wake`
```bash
curl -X POST -H "Content-Type: application/json" -d '{
@@ -23,6 +27,11 @@ curl -X POST -H "Content-Type: application/json" -d '{
curl http://localhost:9999/wake?mac=00:AA:BB:CC:DD:EE
```
### GET to `/targets`
```bash
curl http://localhost:9999/targets
```
### GET to `/wake/:name`
```bash
curl http://localhost:9999/wake/<target name>

31
api.js
View File

@@ -8,7 +8,30 @@ const port = 9999;
app.use(cors()).use(express.json());
app.post('/wake', (req, res) => {
app.get("/", (req, res) => {
let html = `<!DOCTYPE html><head>
<style>
body { background: #fff; color: #000; }
@media (prefers-color-scheme: dark) {
body { background: #111; color: #eee; }
a { color: #7295f6; }
}
</style>
</head>
<body>
<h2 style="margin-bottom: 5px">Targets:</h2>
<ul style="margin: 0; padding-left: 0; list-style-position: inside">
${Object.keys(targets).map(key => `<li><a href="/wake/${key}">${key}</a></li>`).join("")}
</ul>
</body>`;
res.send(html);
});
app.get("/targets", (req, res) => {
res.json(Object.keys(targets));
});
app.post("/wake", (req, res) => {
const { mac } = req.body;
if(!mac) {
@@ -23,7 +46,7 @@ app.post('/wake', (req, res) => {
});
});
app.get('/wake', (req, res) => {
app.get("/wake", (req, res) => {
const mac = req.query.mac;
if(!mac) {
@@ -54,10 +77,6 @@ app.get("/wake/:name", (req, res) => {
});
});
app.get("/targets", (req, res) => {
res.json(Object.keys(targets));
});
app.listen(port, () => {
console.log('WOL server running on port ' + port);
});