עזרה | פיצול קובץ למבנה נכון (route, controller ועוד)
-
יש לי קובץ גדול ואני אשמח לדוגמא איך נכון לחלק אותו בין החלקים הנ"ל.
תודה.import express, { json } from 'express'; import { YemotRouter } from 'yemot-router2'; import { fileURLToPath } from 'url'; import process from 'process'; export const app = express(); import { streets, regions, citys} from './src/utils/streets_data.js'; import { *** } from './src/utils/utils_func.js'; import https from 'https'; import { rejects } from 'assert'; export const router = YemotRouter({ printLog: true, uncaughtErrorHandler: (error, call) => { console.log(`Uncaught error in ${call.req.path} from ${call.phone}. error stack: ${error.stack}`); // do something with the error - like send email to developer, print details log, etc. return call.id_list_message([{ type: 'text', data: 'אירעה שגיאה' }]); // play nice error message to the caller } }); router.events.on('call_hangup', (call) => { console.log(`[addAd.js] call ${call.callId} was hangup`); }); router.events.on('call_continue', (call) => { console.log(`[addAd.js] call ${call.callId} was continue`); }); router.events.on('new_call', (call) => { console.log(`[addAd.js] new call ${call.callId} from ${call.phone}`); }); /** @param {import('yemot-router2').Call} call */ async function callHandler (call) { לוגיקה של יצירת ואישור מודעה /*************************************************************************************** */ // הוספה למאגר מידע console.log("מכין בקשה"); const params = new URLSearchParams({ ****}); const url =*******; console.log("שולח בקשה"); let response = await sendRequest(call, url, "אירעה שגיאה בעת שמירת המודעה"); console.log('Response:', response); if (response.status === "success") { call.id_list_message([{ type: "text", data: response.message }]); } } /*************************************************************************************** */ router.get('/', callHandler); // this must if you want to use post requests (api_url_post=yes) app.use(express.urlencoded({ extended: true })); app.use('/', router); console.log('Node starting...'); const port = 3000; //const isMain = process.argv[1] === app.listen(port, () => { console.log(`example yemot-router2 running on port ${port}`); }); -
controller.js (אם יש הרבה מומלץ לפצל לקבצים לפי תחום וכדומה)
/** @param {import('yemot-router2').Call} call */ async function callHandler (call) { לוגיקה של יצירת ואישור מודעה /*************************************************************************************** */ // הוספה למאגר מידע console.log("מכין בקשה"); const params = new URLSearchParams({ ****}); const url =*******; console.log("שולח בקשה"); let response = await sendRequest(call, url, "אירעה שגיאה בעת שמירת המודעה"); console.log('Response:', response); if (response.status === "success") { call.id_list_message([{ type: "text", data: response.message }]); } }router.js
export const router = YemotRouter({ printLog: true, uncaughtErrorHandler: (error, call) => { console.log(`Uncaught error in ${call.req.path} from ${call.phone}. error stack: ${error.stack}`); // do something with the error - like send email to developer, print details log, etc. return call.id_list_message([{ type: 'text', data: 'אירעה שגיאה' }]); // play nice error message to the caller } }); router.events.on('call_hangup', (call) => { console.log(`[addAd.js] call ${call.callId} was hangup`); }); router.events.on('call_continue', (call) => { console.log(`[addAd.js] call ${call.callId} was continue`); }); router.events.on('new_call', (call) => { console.log(`[addAd.js] new call ${call.callId} from ${call.phone}`); }); router.get('/', callHandler);app.js
// this must if you want to use post requests (api_url_post=yes) app.use(express.urlencoded({ extended: true })); app.use('/', router); console.log('Node starting...'); const port = 3000; app.listen(port, () => { console.log(`example yemot-router2 running on port ${port}`); }); -
controller.js (אם יש הרבה מומלץ לפצל לקבצים לפי תחום וכדומה)
/** @param {import('yemot-router2').Call} call */ async function callHandler (call) { לוגיקה של יצירת ואישור מודעה /*************************************************************************************** */ // הוספה למאגר מידע console.log("מכין בקשה"); const params = new URLSearchParams({ ****}); const url =*******; console.log("שולח בקשה"); let response = await sendRequest(call, url, "אירעה שגיאה בעת שמירת המודעה"); console.log('Response:', response); if (response.status === "success") { call.id_list_message([{ type: "text", data: response.message }]); } }router.js
export const router = YemotRouter({ printLog: true, uncaughtErrorHandler: (error, call) => { console.log(`Uncaught error in ${call.req.path} from ${call.phone}. error stack: ${error.stack}`); // do something with the error - like send email to developer, print details log, etc. return call.id_list_message([{ type: 'text', data: 'אירעה שגיאה' }]); // play nice error message to the caller } }); router.events.on('call_hangup', (call) => { console.log(`[addAd.js] call ${call.callId} was hangup`); }); router.events.on('call_continue', (call) => { console.log(`[addAd.js] call ${call.callId} was continue`); }); router.events.on('new_call', (call) => { console.log(`[addAd.js] new call ${call.callId} from ${call.phone}`); }); router.get('/', callHandler);app.js
// this must if you want to use post requests (api_url_post=yes) app.use(express.urlencoded({ extended: true })); app.use('/', router); console.log('Node starting...'); const port = 3000; app.listen(port, () => { console.log(`example yemot-router2 running on port ${port}`); });@צדיק-תמים תודה
אבל יש לי 4 קבצים, אז לעשות תיקיה מכל סוג ואז בכל תיקיה addAdRoute/controller
והapp הוא מרכזי לכולם? מה אמור להיות בו? -
@צדיק-תמים תודה
אבל יש לי 4 קבצים, אז לעשות תיקיה מכל סוג ואז בכל תיקיה addAdRoute/controller
והapp הוא מרכזי לכולם? מה אמור להיות בו? -
@צדיק-תמים כי יש לי ככה:
- קובץ שאחראי על הוספת מודעות חדשות.
- קובץ שאחראי על ניהול המודעות.
- קובץ שאחראי על מציאת התאמה למודעות/רישום להתראות.
- קובץ שאחראי על ניהול ההתראות של מחפש הדירה.
אני לא יכול לשים את כל זה בקובץ אחד, גם ככה זה בלאגן.
וזה בלי הקובץ של הפונקציות הכלליות והקובץ של המידע
-
@צדיק-תמים כי יש לי ככה:
- קובץ שאחראי על הוספת מודעות חדשות.
- קובץ שאחראי על ניהול המודעות.
- קובץ שאחראי על מציאת התאמה למודעות/רישום להתראות.
- קובץ שאחראי על ניהול ההתראות של מחפש הדירה.
אני לא יכול לשים את כל זה בקובץ אחד, גם ככה זה בלאגן.
וזה בלי הקובץ של הפונקציות הכלליות והקובץ של המידע
@עידו300 לא בטוח שזה לא מתאים לקובץ אחד, בכל מקרה אין בעיה, תעשה תקיית controllers ובה כמה קבצים שתרצה
@עידו300 כתב בעזרה | פיצול קובץ למבנה נכון (route, controller ועוד):
והapp הוא מרכזי לכולם? מה אמור להיות בו?
הוא הקובץ הראשי, אותו בעצם אתה מריץ כדי להריץ את האפליקציה, הוא אמור להכיל דברים גלובליים ממש כמו אתחול אקספרס, הגדרת מידלוורים גלובליים, התחברות לדאטהבייס, וכמובן האזנה על פורט
-
@עידו300 לא בטוח שזה לא מתאים לקובץ אחד, בכל מקרה אין בעיה, תעשה תקיית controllers ובה כמה קבצים שתרצה
@עידו300 כתב בעזרה | פיצול קובץ למבנה נכון (route, controller ועוד):
והapp הוא מרכזי לכולם? מה אמור להיות בו?
הוא הקובץ הראשי, אותו בעצם אתה מריץ כדי להריץ את האפליקציה, הוא אמור להכיל דברים גלובליים ממש כמו אתחול אקספרס, הגדרת מידלוורים גלובליים, התחברות לדאטהבייס, וכמובן האזנה על פורט
@צדיק-תמים כל הimport בקונטרולר נכון?
-
@צדיק-תמים כל הimport בקונטרולר נכון?
-
controller.js (אם יש הרבה מומלץ לפצל לקבצים לפי תחום וכדומה)
/** @param {import('yemot-router2').Call} call */ async function callHandler (call) { לוגיקה של יצירת ואישור מודעה /*************************************************************************************** */ // הוספה למאגר מידע console.log("מכין בקשה"); const params = new URLSearchParams({ ****}); const url =*******; console.log("שולח בקשה"); let response = await sendRequest(call, url, "אירעה שגיאה בעת שמירת המודעה"); console.log('Response:', response); if (response.status === "success") { call.id_list_message([{ type: "text", data: response.message }]); } }router.js
export const router = YemotRouter({ printLog: true, uncaughtErrorHandler: (error, call) => { console.log(`Uncaught error in ${call.req.path} from ${call.phone}. error stack: ${error.stack}`); // do something with the error - like send email to developer, print details log, etc. return call.id_list_message([{ type: 'text', data: 'אירעה שגיאה' }]); // play nice error message to the caller } }); router.events.on('call_hangup', (call) => { console.log(`[addAd.js] call ${call.callId} was hangup`); }); router.events.on('call_continue', (call) => { console.log(`[addAd.js] call ${call.callId} was continue`); }); router.events.on('new_call', (call) => { console.log(`[addAd.js] new call ${call.callId} from ${call.phone}`); }); router.get('/', callHandler);app.js
// this must if you want to use post requests (api_url_post=yes) app.use(express.urlencoded({ extended: true })); app.use('/', router); console.log('Node starting...'); const port = 3000; app.listen(port, () => { console.log(`example yemot-router2 running on port ${port}`); });@צדיק-תמים כתב בעזרה | פיצול קובץ למבנה נכון (route, controller ועוד):
controller.js (אם יש הרבה מומלץ לפצל לקבצים לפי תחום וכדומה)
/** @param {import('yemot-router2').Call} call */ async function callHandler (call) { לוגיקה של יצירת ואישור מודעה /*************************************************************************************** */ // הוספה למאגר מידע console.log("מכין בקשה"); const params = new URLSearchParams({ ****}); const url =*******; console.log("שולח בקשה"); let response = await sendRequest(call, url, "אירעה שגיאה בעת שמירת המודעה"); console.log('Response:', response); if (response.status === "success") { call.id_list_message([{ type: "text", data: response.message }]); } }כאן בקונטרולר צריך לייבא את הספריה של ימות המשיח, לא?
router.js
export const router = YemotRouter({ printLog: true, uncaughtErrorHandler: (error, call) => { console.log(`Uncaught error in ${call.req.path} from ${call.phone}. error stack: ${error.stack}`); // do something with the error - like send email to developer, print details log, etc. return call.id_list_message([{ type: 'text', data: 'אירעה שגיאה' }]); // play nice error message to the caller } }); router.events.on('call_hangup', (call) => { console.log(`[addAd.js] call ${call.callId} was hangup`); }); router.events.on('call_continue', (call) => { console.log(`[addAd.js] call ${call.callId} was continue`); }); router.events.on('new_call', (call) => { console.log(`[addAd.js] new call ${call.callId} from ${call.phone}`); }); router.get('/', callHandler);גם כאן בראוט
app.js
// this must if you want to use post requests (api_url_post=yes) app.use(express.urlencoded({ extended: true })); app.use('/', router); console.log('Node starting...'); const port = 3000; app.listen(port, () => { console.log(`example yemot-router2 running on port ${port}`); });וכאן את של האקספרס
-
@צדיק-תמים כתב בעזרה | פיצול קובץ למבנה נכון (route, controller ועוד):
controller.js (אם יש הרבה מומלץ לפצל לקבצים לפי תחום וכדומה)
/** @param {import('yemot-router2').Call} call */ async function callHandler (call) { לוגיקה של יצירת ואישור מודעה /*************************************************************************************** */ // הוספה למאגר מידע console.log("מכין בקשה"); const params = new URLSearchParams({ ****}); const url =*******; console.log("שולח בקשה"); let response = await sendRequest(call, url, "אירעה שגיאה בעת שמירת המודעה"); console.log('Response:', response); if (response.status === "success") { call.id_list_message([{ type: "text", data: response.message }]); } }כאן בקונטרולר צריך לייבא את הספריה של ימות המשיח, לא?
router.js
export const router = YemotRouter({ printLog: true, uncaughtErrorHandler: (error, call) => { console.log(`Uncaught error in ${call.req.path} from ${call.phone}. error stack: ${error.stack}`); // do something with the error - like send email to developer, print details log, etc. return call.id_list_message([{ type: 'text', data: 'אירעה שגיאה' }]); // play nice error message to the caller } }); router.events.on('call_hangup', (call) => { console.log(`[addAd.js] call ${call.callId} was hangup`); }); router.events.on('call_continue', (call) => { console.log(`[addAd.js] call ${call.callId} was continue`); }); router.events.on('new_call', (call) => { console.log(`[addAd.js] new call ${call.callId} from ${call.phone}`); }); router.get('/', callHandler);גם כאן בראוט
app.js
// this must if you want to use post requests (api_url_post=yes) app.use(express.urlencoded({ extended: true })); app.use('/', router); console.log('Node starting...'); const port = 3000; app.listen(port, () => { console.log(`example yemot-router2 running on port ${port}`); });וכאן את של האקספרס
-
@צדיק-תמים לcallHandler צריך לעשות export וליבא אותו בראוט?
-
@צדיק-תמים לcallHandler צריך לעשות export וליבא אותו בראוט?