typescript server api

This commit is contained in:
2023-11-10 23:29:17 +01:00
parent 8a34b043e0
commit 5289b85802
6 changed files with 1049 additions and 53 deletions

1
.nvmrc Normal file
View File

@@ -0,0 +1 @@
v18

View File

@@ -4,4 +4,4 @@ COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "server"]
CMD ["npm", "run", "server"]

1000
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,9 @@
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --noEmit",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
"server": "node --no-warnings --loader ts-node/esm server/index.ts",
"server:dev": "nodemon --watch \"server/**\" --ext \"ts\" --exec \"npm run server\""
},
"dependencies": {
"@vueuse/core": "^10.5.0",
@@ -23,6 +25,7 @@
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.5.1",
"@types/express": "^4.17.21",
"@types/node": "^20.9.0",
"@vitejs/plugin-vue": "^4.4.1",
"@vue/eslint-config-prettier": "^7.0.0",
@@ -30,10 +33,13 @@
"@vue/tsconfig": "^0.4.0",
"eslint": "^8.53.0",
"eslint-plugin-vue": "^9.18.1",
"nodemon": "^3.0.1",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"ts-node": "^10.9.1",
"typescript": "^5.2.2",
"vite": "^4.5.0",
"vue-tsc": "^1.8.22"
}
},
"type": "module"
}

View File

@@ -1,22 +1,29 @@
/* eslint-env node */
require('dotenv').config()
const express = require('express')
const app = express()
const path = require('path')
const mongo = require('mongodb')
const MongoClient = mongo.MongoClient
import dotenv from 'dotenv'
import express from 'express'
import type { Request, Response } from 'express'
import mongo from 'mongodb'
import path from 'path'
import { fileURLToPath } from 'url'
dotenv.config()
console.log('Starting with parameters:')
const parameters = ['PORT', 'MONGO_SERVER', 'MONGO_DB']
parameters.forEach((parameter) => console.log(`${parameter}=${process.env[parameter]}`))
const app = express()
app.listen(process.env.PORT, () => {
console.log('Listening on ' + process.env.PORT)
})
let db
let db: mongo.Db
const mongoClient = mongo.MongoClient
async function connectToMongo() {
try {
const mongoUrl = `mongodb://${process.env.MONGO_SERVER}:27017`
const client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true })
const client = await mongoClient.connect(mongoUrl)
db = client.db(process.env.MONGO_DB)
console.log('Connected succesfully to server')
} catch (e) {
@@ -27,15 +34,19 @@ async function connectToMongo() {
connectToMongo()
app.use(function (req, res, next) {
function objectIdFromDate(date: Date) {
return new mongo.ObjectId(Math.floor(date.getTime() / 1000).toString(16) + '0000000000000000')
}
app.use(function (req: Request, res: Response, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req, res) => {
const startDate = new Date(req.params.startDate * 1000)
const endDate = new Date(req.params.endDate * 1000)
app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req: Request, res: Response) => {
const startDate = new Date(parseInt(req.params.startDate) * 1000)
const endDate = new Date(parseInt(req.params.endDate) * 1000)
const sample = parseInt(req.params.sample)
const type = req.params.type
const agg = [
@@ -79,45 +90,11 @@ app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', asyn
}
})
// app.get('/v0/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req, res) => {
// const startDate = new Date(req.params.startDate * 1000)
// const endDate = new Date(req.params.endDate * 1000)
// const sample = parseInt(req.params.sample)
// const query = {
// _id: {
// $gt: objectIdFromDate(startDate),
// $lt: objectIdFromDate(endDate)
// },
// type: req.params.type,
// value: {
// $ne: NaN
// }
// }
// try {
// const docs = await db.collection('dht22').find(query).toArray()
// const sampledDocs = docs
// .filter((value, index) => index % sample === 0)
// .map((doc) => {
// return { ...doc, date: dateFromObjectId(doc._id) }
// })
// res.json(sampledDocs)
// } catch (err) {
// console.log(err)
// }
// })
function objectIdFromDate(date) {
return mongo.ObjectId(Math.floor(date.getTime() / 1000).toString(16) + '0000000000000000')
}
// function dateFromObjectId(objectId) {
// return Math.floor(mongo.ObjectId(objectId).getTimestamp().getTime() / 1000)
// }
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
app.use(express.static(path.join(__dirname, '../dist')))
app.get('/*', function (req, res) {
app.get('/*', function (req: Request, res: Response) {
res.sendFile(path.join(__dirname, '../dist/index.html'))
})
module.exports = app

12
server/tsconfig.json Normal file
View File

@@ -0,0 +1,12 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "ESNext", /* Specify what module code is generated. */
"moduleResolution": "NodeNext", /* Specify how TypeScript looks up a file from a given module specifier. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}