Started switching to CommonJS

import to require
This commit is contained in:
2020-10-07 21:18:09 +02:00
parent 8437ea4682
commit 871919c838
7 changed files with 47 additions and 95 deletions

View File

@@ -1,8 +1,8 @@
import * as config from "./config/index.js"; const config = require("./config/index.js");
const { port, dbPass, cookiesSecret } = config; const { port, dbPass, cookiesSecret } = config;
import express from "express"; const express = require("express");
const app = express(); const app = express();
import loaders from "./loaders/index.js"; const loaders = require("./loaders/index.js");
// //
// Server init function // Server init function
// //

View File

@@ -1,15 +1,15 @@
import dotenv from "dotenv"; const dotenv = require("dotenv");
dotenv.config(); dotenv.config();
export const port = process.env.PORT; exports.port = process.env.PORT;
export const dbPass = process.env.DB_PASS; exports.dbPass = process.env.DB_PASS;
export const dbUser = process.env.DB_USER; exports.dbUser = process.env.DB_USER;
export const dbHost = process.env.DB_HOST; exports.dbHost = process.env.DB_HOST;
export const dbPort = process.env.DB_PORT; exports.dbPort = process.env.DB_PORT;
export const dbName = process.env.DB_NAME; exports.dbName = process.env.DB_NAME;
export const cookiesSecret = process.env.COOKIES_SECRET; exports.cookiesSecret = process.env.COOKIES_SECRET;
export const jwtSecret = process.env.JWT_SECRET; exports.jwtSecret = process.env.JWT_SECRET;
export const CRM_KEY = process.env.CRM_KEY; exports.CRM_KEY = process.env.CRM_KEY;
export const CRM_USER = process.env.CRM_USER; exports.CRM_USER = process.env.CRM_USER;
export const CRM_EMAIL = process.env.CRM_EMAIL; exports.CRM_EMAIL = process.env.CRM_EMAIL;
export const MAIL_PASS = process.env.MAIL_PASS; exports.MAIL_PASS = process.env.MAIL_PASS;

View File

@@ -1,18 +1,18 @@
import bodyParser from "body-parser"; const bodyParser = require("body-parser");
import cors from "cors"; const cors = require("cors");
import rateLimiter from "express-rate-limit"; const rateLimiter = require("express-rate-limit");
import helmet from "helmet"; const helmet = require("helmet");
import routeDish from "../routes/routeDish.js"; const routeDish = require("../routes/routeDish.js");
import routeRestaurant from "../routes/routeRestaurant.js"; const routeRestaurant = require("../routes/routeRestaurant.js");
import routeUser from "../routes/routeUser.js"; const routeUser = require("../routes/routeUser.js");
import routeSearch from "../routes/routeSearch.js"; const routeSearch = require("../routes/routeSearch.js");
import routeImg from "../routes/routeImg.js"; const routeImg = require("../routes/routeImg.js");
import routePayments from "../routes/routePayments.js"; const routePayments = require("../routes/routePayments.js");
export default ({ app, secret }) => { const loadExpress = ({ app, secret }) => {
const limiter = rateLimiter({ const limiter = rateLimiter({
windowMs: 15 * 60 * 1000, //time window windowMs: 15 * 60 * 1000, //time window
max: 100, //requests from a single IP for a time window max: 100, //requests = a single IP for a time window
}); });
app.use(cors({ exposedHeaders: "x-auth-token" })); app.use(cors({ exposedHeaders: "x-auth-token" }));
@@ -32,3 +32,5 @@ export default ({ app, secret }) => {
return app; return app;
}; };
module.exports = loadExpress;

View File

@@ -1,9 +1,11 @@
import expressLoader from "./express.js"; const expressLoader = require("./express.js");
import mongooseLoader from "./mongoose.js"; const mongooseLoader = require("./mongoose.js");
export default async ({ expressApp, secret }) => { const loaders = async ({ expressApp, secret }) => {
const mongoConnection = await mongooseLoader(); const mongoConnection = await mongooseLoader();
console.log("Mongoose Loaded"); console.log("Mongoose Loaded");
await expressLoader({ app: expressApp, secret: secret }); await expressLoader({ app: expressApp, secret: secret });
console.log("Express Initialized"); console.log("Express Initialized");
}; };
module.exports = loaders;

View File

@@ -1,7 +1,13 @@
import mongoose from "mongoose"; const mongoose = require("mongoose");
import { dbPass, dbUser, dbHost, dbPort, dbName } from "../config/index.js"; const {
dbPass,
dbUser,
dbHost,
dbPort,
dbName,
} = require("../config/index.js");
export default async () => { const loadMongoose = async () => {
const connection = await mongoose.connect( const connection = await mongoose.connect(
"mongodb://" + "mongodb://" +
dbHost + dbHost +
@@ -25,3 +31,5 @@ export default async () => {
} }
); );
}; };
module.exports = loadMongoose;

View File

@@ -2,12 +2,11 @@
"name": "menui_backend", "name": "menui_backend",
"version": "1.0.0", "version": "1.0.0",
"description": "backend for Menui", "description": "backend for Menui",
"main": "app.mjs", "main": "app.js",
"scripts": { "scripts": {
"test": "jest" "test": "jest"
}, },
"keywords": [], "keywords": [],
"type": "module",
"author": "Jonasz Bigda", "author": "Jonasz Bigda",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {

View File

@@ -1,59 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This configuration file is required if iisnode is used to run node processes behind
IIS or IIS Express. For more information, visit:
https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->
<configuration>
<system.webServer>
<!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
<webSocket enabled="false" />
<handlers>
<!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
<add name="iisnode" path="app.mjs" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<!-- Do not interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^app.mjs\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<!-- All other URLs are mapped to the node.js site entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="server.js"/>
</rule>
</rules>
</rewrite>
<!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>
<!-- Make sure error responses are left untouched -->
<httpErrors existingResponse="PassThrough" />
<!--
You can control how Node is hosted within IIS using the following options:
* watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
* node_env: will be propagated to node as NODE_ENV environment variable
* debuggingEnabled - controls whether the built-in debugger is enabled
See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
-->
<!--<iisnode watchedFiles="web.config;*.js"/>-->
</system.webServer>
</configuration>