typescript server api
This commit is contained in:
@@ -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
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