start pow server work

This commit is contained in:
smolgrrr 2023-11-18 00:23:02 +11:00
parent f7f4fd1278
commit ec2a37d6c2
4 changed files with 69 additions and 0 deletions

2
server/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
bun.lockb

33
server/index.ts Normal file
View File

@ -0,0 +1,33 @@
import express from 'express';
import bodyParser from 'body-parser';
import { minePow } from 'nostr-tools/lib/types/nip13';
const app = express();
const port = 3000;
// Use body-parser middleware to parse JSON requests
app.use(bodyParser.json());
app.post('/powgen', async (req, res) => {
try {
const { unsigned, difficulty } = req.body;
// Validate input
if (!unsigned || !difficulty) {
return res.status(400).send('Missing unsigned event or difficulty.');
}
// Call minePow function to generate PoW
const result = minePow(unsigned, difficulty);
// Send back the result
res.json(result);
} catch (error) {
console.error('Error generating PoW:', error);
res.status(500).send('Internal server error');
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});

21
server/package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "tsc && node dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.21",
"@types/node": "^20.9.1",
"express": "^4.18.2",
"nostr-tools": "^1.17.0",
"typescript": "^5.2.2"
}
}

13
server/tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"rootDir": "./",
"outDir": "./dist",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*", "index.ts"],
"exclude": ["node_modules"]
}