Compare commits
2 Commits
d5c9465f8a
...
c917fd739a
| Author | SHA1 | Date | |
|---|---|---|---|
| c917fd739a | |||
| 6d39c4b8e9 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -138,4 +138,6 @@ dist
|
||||
# Vite files
|
||||
vite.config.js.timestamp-*
|
||||
vite.config.ts.timestamp-*
|
||||
.vite/
|
||||
.vite/
|
||||
|
||||
targets.json
|
||||
10
README.md
10
README.md
@@ -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 '{
|
||||
@@ -18,4 +21,9 @@ curl -X POST -H "Content-Type: application/json" -d '{
|
||||
### GET to `/wake`
|
||||
```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
21
api.js
@@ -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
4
targets.example.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"pc": "00:AA:BB:CC:DD:EE",
|
||||
"server": "01:23:45:67:89:00"
|
||||
}
|
||||
Reference in New Issue
Block a user