Compare commits

...

2 Commits

Author SHA1 Message Date
c917fd739a docs: update README with /wake/:name request example 2026-01-28 02:02:26 +01:00
6d39c4b8e9 feat: add name based endpoints 2026-01-28 02:00:48 +01:00
4 changed files with 37 additions and 2 deletions

2
.gitignore vendored
View File

@@ -139,3 +139,5 @@ dist
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
.vite/
targets.json

View File

@@ -6,8 +6,11 @@ Wake-on-LAN API in Node.js
1. `npm install`
2. `npm run start`
## Usage
## Custom targets
1. Copy `targets.example.json` to `targets.json`
2. Edit the file and add your targets
## Usage
### POST to `/wake`
```bash
curl -X POST -H "Content-Type: application/json" -d '{
@@ -19,3 +22,8 @@ curl -X POST -H "Content-Type: application/json" -d '{
```bash
curl http://localhost:9999/wake?mac=00:AA:BB:CC:DD:EE
```
### GET to `/wake/:name`
```bash
curl http://localhost:9999/wake/<target name>
```

21
api.js
View File

@@ -1,6 +1,7 @@
import express from 'express';
import cors from 'cors';
import { sendWolPacket } from './wol.js';
import targets from "./targets.json" with { type: "json" };
const app = express();
const port = 9999;
@@ -37,6 +38,26 @@ app.get('/wake', (req, res) => {
});
});
app.get("/wake/:name", (req, res) => {
const name = req.params.name;
const mac = targets[name];
if (!mac) {
return res.status(404).json({ status: "error", message: "Unknown target" });
}
sendWolPacket(mac)
.then(() => res.json({ status: "ok" }))
.catch(err => {
console.error(err);
res.status(500).json({ status: "error", message: "Failed to send WOL packet" });
});
});
app.get("/targets", (req, res) => {
res.json(Object.keys(targets));
});
app.listen(port, () => {
console.log('WOL server running on port ' + port);
});

4
targets.example.json Normal file
View File

@@ -0,0 +1,4 @@
{
"pc": "00:AA:BB:CC:DD:EE",
"server": "01:23:45:67:89:00"
}