alles auf Module (import) umgeschrieben

This commit is contained in:
rxf
2026-01-16 16:24:41 +01:00
parent ec953b0ae6
commit 72c806be24
9 changed files with 179 additions and 39 deletions

3
.vscode/launch.json vendored
View File

@@ -13,7 +13,8 @@
],
"program": "${workspaceFolder}/bin/www",
"env": {
"MONGOPORT": "27017"
"MONGOPORT": "27017",
"DEBUG":"spritzschema:server"
}
}
]

View File

@@ -1,4 +1,4 @@
FROM node:20-alpine
FROM node:alpine
ADD package.json /tmp
RUN cd /tmp && npm install
@@ -8,12 +8,7 @@ WORKDIR /opt/app
ADD . /opt/app
RUN apk add --no-cache tzdata
ENV TZ Europe/Berlin
ENV TZ=Europe/Berlin
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN touch cmds.sh \
&& echo 'npm start' >>cmds.sh
EXPOSE 3014
CMD sh ./cmds.sh
CMD ["npm", "start"]

22
app.js
View File

@@ -1,13 +1,25 @@
import createError from 'http-errors'
import logger from 'morgan'
import express from 'express'
import cookieParser from 'cookie-parser'
import path from 'path'
import { fileURLToPath } from 'url';
/*
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
*/
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
import indexRouter from './routes/index.js'
// import usersRouter from './routes/users.js'
var app = express();
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
@@ -21,7 +33,7 @@ app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/data', indexRouter);
app.use('/users', usersRouter);
// app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
@@ -39,4 +51,4 @@ app.use(function(err, req, res, next) {
res.render('error');
});
module.exports = app;
export default app

98
bin/www.js Executable file
View File

@@ -0,0 +1,98 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
import app from '../app.js'
import Debug from 'debug'
import http from 'http'
const debug = Debug('spritzschema:server')
/*
var app = require('../app');
var debug = require('debug')('Spritzschema:server');
var http = require('http');
*/
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3200');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
//debug('Listening on ' + bind);
console.log('Listening on ' + bind);
}

View File

@@ -15,14 +15,16 @@ services:
# command: '--auth'
restart: unless-stopped
# spritzschema:
# image: spritzschema
# volumes:
# - ${PWD}/log:/var/log
spritzschema:
build:
context: .
dockerfile: Dockerfile_spritzschema
volumes:
- ${PWD}/log:/var/log
#ports:
# - '3100:3100'
# container_name: spritzschema
# environment:
# - MONGOHOST=mongodb
# restart: unless-stopped
#
# - '3100:3014'
container_name: spritzschema
environment:
- MONGOHOST=mongodb
restart: unless-stopped

View File

@@ -0,0 +1,30 @@
version: '3.9'
volumes:
mongodata:
services:
mongodb:
image: mongo
volumes:
- mongodata:/data/db
- ${PWD}/log:/var/log
ports:
- "27017:27017"
container_name: mongodb
# command: '--auth'
restart: unless-stopped
#spritzschema:
# build:
# context: .
# dockerfile: Dockerfile_spritzschema
# volumes:
# - ${PWD}/log:/var/log
# #ports:
# # - '3100:3014'
# container_name: spritzschema
# environment:
# - MONGOHOST=mongodb
# restart: unless-stopped

View File

@@ -1,6 +1,6 @@
// all functions to get data from mongodb
const {MongoClient} = require('mongodb')
import { MongoClient } from 'mongodb';
const MONGOHOST = process.env.MONGOHOST || 'localhost'
const MONGOPORT = process.env.MONGOPORT || 27017
@@ -11,7 +11,7 @@ const MONGOBASE = process.env.MONGOBASE || 'medizin'
const MONGO_URL = MONGOAUTH ? 'mongodb://'+MONGOUSRP+'@' + MONGOHOST + ':' + MONGOPORT + '/?authSource=admin' : 'mongodb://'+MONGOHOST+':'+MONGOPORT // URL to mongo database
let COLLECTION = 'spritzschema'
const doMongo = async function(cmd, options) {
export const doMongo = async function(cmd, options) {
let erg = {err: null}
if (options.testing) {
COLLECTION = 'spritzschema_test'
@@ -95,4 +95,4 @@ async function delAPIdataOne(client, options) {
}
return erg
}
exports.doMongo = doMongo

View File

@@ -2,9 +2,10 @@
"name": "spritzschema",
"version": "1.2.2",
"date": "2023-07-06",
"type": "module",
"private": true,
"scripts": {
"start": "node ./bin/www >>/var/log/spritzschema.log 2>&1"
"start": "node ./bin/www.js >>/var/log/spritzschema.log 2>&1"
},
"dependencies": {
"cookie-parser": "~1.4.4",

View File

@@ -1,7 +1,8 @@
const express = require('express');
import express from 'express'
import { doMongo } from '../modules/mongointerface.js'
import pkg from '../package.json' with { type: "json" }
const router = express.Router();
const domongo = require('../modules/mongointerface')
const pkg = require('../package.json')
/* GET home page. */
router.get('/', function(req, res, next) {
@@ -19,7 +20,7 @@ router.get('/data', async function(req, res, next) {
const options = {}
options.curdate = req.query.curdate
options.testing = req.query.test
let erg = await domongo.doMongo('getlastdata', options)
let erg = await doMongo('getlastdata', options)
res.json(erg)
})
@@ -27,8 +28,8 @@ router.post('/data', async function (req, res, next) {
const options = {}
options.data = req.body
options.testing = req.query.test
let erg = await domongo.doMongo('putdata', options)
let erg = await doMongo('putdata', options)
res.json(erg)
})
module.exports = router;
export default router