typescript server api
This commit is contained in:
@@ -4,4 +4,4 @@ COPY package*.json ./
|
|||||||
RUN npm ci
|
RUN npm ci
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
CMD ["node", "server"]
|
CMD ["npm", "run", "server"]
|
||||||
|
|||||||
1000
package-lock.json
generated
1000
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -7,7 +7,9 @@
|
|||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
"build-only": "vite build",
|
"build-only": "vite build",
|
||||||
"type-check": "vue-tsc --noEmit",
|
"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": {
|
"dependencies": {
|
||||||
"@vueuse/core": "^10.5.0",
|
"@vueuse/core": "^10.5.0",
|
||||||
@@ -23,6 +25,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rushstack/eslint-patch": "^1.5.1",
|
"@rushstack/eslint-patch": "^1.5.1",
|
||||||
|
"@types/express": "^4.17.21",
|
||||||
"@types/node": "^20.9.0",
|
"@types/node": "^20.9.0",
|
||||||
"@vitejs/plugin-vue": "^4.4.1",
|
"@vitejs/plugin-vue": "^4.4.1",
|
||||||
"@vue/eslint-config-prettier": "^7.0.0",
|
"@vue/eslint-config-prettier": "^7.0.0",
|
||||||
@@ -30,10 +33,13 @@
|
|||||||
"@vue/tsconfig": "^0.4.0",
|
"@vue/tsconfig": "^0.4.0",
|
||||||
"eslint": "^8.53.0",
|
"eslint": "^8.53.0",
|
||||||
"eslint-plugin-vue": "^9.18.1",
|
"eslint-plugin-vue": "^9.18.1",
|
||||||
|
"nodemon": "^3.0.1",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"prettier": "^3.0.3",
|
"prettier": "^3.0.3",
|
||||||
|
"ts-node": "^10.9.1",
|
||||||
"typescript": "^5.2.2",
|
"typescript": "^5.2.2",
|
||||||
"vite": "^4.5.0",
|
"vite": "^4.5.0",
|
||||||
"vue-tsc": "^1.8.22"
|
"vue-tsc": "^1.8.22"
|
||||||
}
|
},
|
||||||
|
"type": "module"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,29 @@
|
|||||||
/* eslint-env node */
|
import dotenv from 'dotenv'
|
||||||
require('dotenv').config()
|
import express from 'express'
|
||||||
const express = require('express')
|
import type { Request, Response } from 'express'
|
||||||
const app = express()
|
import mongo from 'mongodb'
|
||||||
const path = require('path')
|
import path from 'path'
|
||||||
const mongo = require('mongodb')
|
import { fileURLToPath } from 'url'
|
||||||
const MongoClient = mongo.MongoClient
|
|
||||||
|
dotenv.config()
|
||||||
|
|
||||||
console.log('Starting with parameters:')
|
console.log('Starting with parameters:')
|
||||||
const parameters = ['PORT', 'MONGO_SERVER', 'MONGO_DB']
|
const parameters = ['PORT', 'MONGO_SERVER', 'MONGO_DB']
|
||||||
parameters.forEach((parameter) => console.log(`${parameter}=${process.env[parameter]}`))
|
parameters.forEach((parameter) => console.log(`${parameter}=${process.env[parameter]}`))
|
||||||
|
|
||||||
|
const app = express()
|
||||||
|
|
||||||
app.listen(process.env.PORT, () => {
|
app.listen(process.env.PORT, () => {
|
||||||
console.log('Listening on ' + process.env.PORT)
|
console.log('Listening on ' + process.env.PORT)
|
||||||
})
|
})
|
||||||
|
|
||||||
let db
|
let db: mongo.Db
|
||||||
|
const mongoClient = mongo.MongoClient
|
||||||
|
|
||||||
async function connectToMongo() {
|
async function connectToMongo() {
|
||||||
try {
|
try {
|
||||||
const mongoUrl = `mongodb://${process.env.MONGO_SERVER}:27017`
|
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)
|
db = client.db(process.env.MONGO_DB)
|
||||||
console.log('Connected succesfully to server')
|
console.log('Connected succesfully to server')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -27,15 +34,19 @@ async function connectToMongo() {
|
|||||||
|
|
||||||
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-Origin', '*')
|
||||||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req, res) => {
|
app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req: Request, res: Response) => {
|
||||||
const startDate = new Date(req.params.startDate * 1000)
|
const startDate = new Date(parseInt(req.params.startDate) * 1000)
|
||||||
const endDate = new Date(req.params.endDate * 1000)
|
const endDate = new Date(parseInt(req.params.endDate) * 1000)
|
||||||
const sample = parseInt(req.params.sample)
|
const sample = parseInt(req.params.sample)
|
||||||
const type = req.params.type
|
const type = req.params.type
|
||||||
const agg = [
|
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 __filename = fileURLToPath(import.meta.url)
|
||||||
// const startDate = new Date(req.params.startDate * 1000)
|
const __dirname = path.dirname(__filename)
|
||||||
// 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)
|
|
||||||
// }
|
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname, '../dist')))
|
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'))
|
res.sendFile(path.join(__dirname, '../dist/index.html'))
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = app
|
|
||||||
12
server/tsconfig.json
Normal file
12
server/tsconfig.json
Normal 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. */
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user