add typescript api skeleton
parent
8894f7d7ae
commit
6ed041d22e
@ -1,3 +1,5 @@
|
|||||||
monero-lws
|
monero-lws
|
||||||
docker-monero-node
|
docker-monero-node
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
node_modules
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "src/server.ts",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "nodemon src/server.ts",
|
||||||
|
"build": "rm -rf build/ && prettier --write src/ && tsc"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/axios": "^0.14.0",
|
||||||
|
"@types/express": "^4.17.17",
|
||||||
|
"@types/morgan": "^1.9.4",
|
||||||
|
"axios": "^1.4.0",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"morgan": "^1.10.0",
|
||||||
|
"nodemon": "^2.0.22",
|
||||||
|
"ts-node": "^10.9.1",
|
||||||
|
"typescript": "^5.0.4"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"name": "monero-lws-backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"engines": {
|
||||||
|
"node": "16.x"
|
||||||
|
},
|
||||||
|
"description": "",
|
||||||
|
"main": "serve.ts",
|
||||||
|
"scripts": {
|
||||||
|
"serve": "node --max_old_space_size=128 --optimize_for_size src/server.js",
|
||||||
|
"start": "concurrently npm:scrape npm:serve --restart-tries -1 --restart-after 5000",
|
||||||
|
"stop": "pkill -e -f concurrently && pkill -e -f scrape",
|
||||||
|
"scrape": "node --max_old_space_size=128 --optimize_for_size src/scraper.js",
|
||||||
|
"resync": "echo Deleting data in 5 seconds... && sleep 5 && rm storage/*.txt",
|
||||||
|
"wipe": "echo Deleting data in 5 seconds... && sleep 5 && npm run resync && storage/*.db"
|
||||||
|
},
|
||||||
|
"author": "lza_menace",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"better-sqlite3": "^7.4.5",
|
||||||
|
"bignumber.js": "^9.0.1",
|
||||||
|
"concurrently": "^6.5.0",
|
||||||
|
"dotenv": "^10.0.0",
|
||||||
|
"ethers": "^5.6.9",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"node-fetch": "^3.2.9",
|
||||||
|
"sqlite3": "^5.0.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,79 @@
|
|||||||
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import axios, { AxiosResponse } from 'axios';
|
||||||
|
|
||||||
|
interface Post {
|
||||||
|
userId: Number;
|
||||||
|
id: Number;
|
||||||
|
title: String;
|
||||||
|
body: String;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getting all posts
|
||||||
|
const getPosts = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
// get some posts
|
||||||
|
let result: AxiosResponse = await axios.get(`https://jsonplaceholder.typicode.com/posts`);
|
||||||
|
let posts: [Post] = result.data;
|
||||||
|
return res.status(200).json({
|
||||||
|
message: posts
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// getting a single post
|
||||||
|
const getPost = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
// get the post id from the req
|
||||||
|
let id: string = req.params.id;
|
||||||
|
// get the post
|
||||||
|
let result: AxiosResponse = await axios.get(`https://jsonplaceholder.typicode.com/posts/${id}`);
|
||||||
|
let post: Post = result.data;
|
||||||
|
return res.status(200).json({
|
||||||
|
message: post
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// updating a post
|
||||||
|
const updatePost = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
// get the post id from the req.params
|
||||||
|
let id: string = req.params.id;
|
||||||
|
// get the data from req.body
|
||||||
|
let title: string = req.body.title ?? null;
|
||||||
|
let body: string = req.body.body ?? null;
|
||||||
|
// update the post
|
||||||
|
let response: AxiosResponse = await axios.put(`https://jsonplaceholder.typicode.com/posts/${id}`, {
|
||||||
|
...(title && { title }),
|
||||||
|
...(body && { body })
|
||||||
|
});
|
||||||
|
// return response
|
||||||
|
return res.status(200).json({
|
||||||
|
message: response.data
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// deleting a post
|
||||||
|
const deletePost = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
// get the post id from req.params
|
||||||
|
let id: string = req.params.id;
|
||||||
|
// delete the post
|
||||||
|
let response: AxiosResponse = await axios.delete(`https://jsonplaceholder.typicode.com/posts/${id}`);
|
||||||
|
// return response
|
||||||
|
return res.status(200).json({
|
||||||
|
message: 'post deleted successfully'
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// adding a post
|
||||||
|
const addPost = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
// get the data from req.body
|
||||||
|
let title: string = req.body.title;
|
||||||
|
let body: string = req.body.body;
|
||||||
|
// add the post
|
||||||
|
let response: AxiosResponse = await axios.post(`https://jsonplaceholder.typicode.com/posts`, {
|
||||||
|
title,
|
||||||
|
body
|
||||||
|
});
|
||||||
|
// return response
|
||||||
|
return res.status(200).json({
|
||||||
|
message: response.data
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default { getPosts, getPost, updatePost, deletePost, addPost };
|
@ -0,0 +1,12 @@
|
|||||||
|
/** source/routes/posts.ts */
|
||||||
|
import express from 'express';
|
||||||
|
import controller from '../interfaces/main';
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get('/posts', controller.getPosts);
|
||||||
|
router.get('/posts/:id', controller.getPost);
|
||||||
|
router.put('/posts/:id', controller.updatePost);
|
||||||
|
router.delete('/posts/:id', controller.deletePost);
|
||||||
|
router.post('/posts', controller.addPost);
|
||||||
|
|
||||||
|
export = router;
|
@ -0,0 +1,34 @@
|
|||||||
|
/** source/server.ts */
|
||||||
|
import http from 'http';
|
||||||
|
import express, { Express } from 'express';
|
||||||
|
import morgan from 'morgan';
|
||||||
|
import routes from './routes/main';
|
||||||
|
|
||||||
|
const router: Express = express();
|
||||||
|
|
||||||
|
router.use(morgan('dev'));
|
||||||
|
router.use(express.urlencoded({ extended: false }));
|
||||||
|
router.use(express.json());
|
||||||
|
|
||||||
|
router.use((req, res, next) => {
|
||||||
|
res.header('Access-Control-Allow-Origin', '*');
|
||||||
|
res.header('Access-Control-Allow-Headers', 'origin, X-Requested-With, Content-Type, Accept, Authorization');
|
||||||
|
if (req.method === 'OPTIONS') {
|
||||||
|
res.header('Access-Control-Allow-Methods', 'GET PATCH DELETE POST');
|
||||||
|
return res.status(200).json({});
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
router.use('/', routes);
|
||||||
|
|
||||||
|
router.use((req, res, next) => {
|
||||||
|
const error = new Error('not found');
|
||||||
|
return res.status(404).json({
|
||||||
|
message: error.message
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const httpServer = http.createServer(router);
|
||||||
|
const PORT: any = process.env.PORT ?? 8082;
|
||||||
|
httpServer.listen(PORT, () => console.log(`The server is running on port ${PORT}`));
|
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"module": "commonjs",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"outDir": "./build",
|
||||||
|
"rootDir": "./src",
|
||||||
|
"target": "es6",
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"strict": true
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue