Hallo zusammen,
ich übe momentan mit Node.js und express. Ich versuche ein einfachen Fahrrad Konfigurator zu erstellen.
Soweit läuft auch alles. Ich habe eine mock.js Datei erstellt in der sich mein Objekt Array befindet. Dieses exportiere ich nun mit module.exports.
In der bike.js versuche ich nun auf dieses Objekt zu zugreifen, nur funktioniert das leider nicht. Wenn ich jedoch das Objekt direkt in die bike.js schreibe, funktioniert es. Was mache ich falsch. Alle Dateien bis auf die app.js befinden sich in einem Ordner.
[CODE lang="javascript" title="bike,js"]var bikeArray = require('./mock');
console.log(bikeArray.model);
window.onload = function () {
var typSel = document.getElementById("typSel"),
modelSel = document.getElementById("modelSel"),
sizeSel = document.getElementById("sizeSel"),
colorSel = document.getElementById("colorSel"),
breakSel = document.getElementById("breakSel"),
tireSel = document.getElementById("tireSel");
for (var typ in bikeArray) {
typSel.options[typSel.options.length] = new Option(typ, typ);
}
//Give Model
typSel.onchange = function () {
modelSel.length = 1;
sizeSel.length = 1;
if (this.selectedIndex < 1) return;
for (var model in bikeArray[this.value]) {
modelSel.options[modelSel.options.length] = new Option(model, model);
}
};
typSel.onchange();
//Give Size
modelSel.onchange = function () {
sizeSel.length = 1;
colorSel.length = 1;
if (this.selectedIndex < 1) return;
var size = bikeArray[typSel.value][this.value];
console.log(size);
for (var s = 0; s < size.Größe.length; s++) {
sizeSel.options[sizeSel.options.length] = new Option(
size.Größe,
size.Größe
);
}
};
//Give Color
sizeSel.onchange = function () {
colorSel.length = 1;
breakSel.length = 1;
if (this.selectedIndex < 1) return;
var color = bikeArray[typSel.value][modelSel.value];
console.log(color);
for (var c = 0; c < color.Farbe.length; c++) {
colorSel.options[colorSel.options.length] = new Option(
color.Farbe[c],
color.Farbe[c]
);
}
};
//Give Breaks
colorSel.onchange = function () {
breakSel.length = 1;
tireSel.length = 1;
if (this.selectedIndex < 1) return;
var breaks = bikeArray[typSel.value][modelSel.value];
console.log(breaks);
for (var b = 0; b < breaks.Bremsen.length; b++) {
breakSel.options[breakSel.options.length] = new Option(
breaks.Bremsen,
breaks.Bremsen
);
}
};
//Give Tires
breakSel.onchange = function () {
tireSel.length = 1;
if (this.selectedIndex < 1) return;
var tires = bikeArray[typSel.value][modelSel.value];
for (var t = 0; t < tires.Reifen.length; t++) {
tireSel.options[tireSel.options.length] = new Option(
tires.Reifen[t],
tires.Reifen[t]
);
}
};
};[/CODE]
[CODE lang="javascript" title="mock.js"]var bikeArray = {
Mountainbike: {
Cross: {
Größe: ["395 mm", "440 mm", "480 mm", "535 mm"],
Farbe: ["Himmelblau", "Grasgrün", "Stahlgrau", "Weinrot"],
Bremsen: [
"Shimano Deore BRM6000",
"Shimano Deore BRM7000",
"Shimano RX810 1x11",
],
Schaltung: [
"Shimano GRX 600 2x11",
"Shimano GRX 600 2x11",
"Shimano GRX 810 Di2 2x11",
],
Reifen: [
"Continental X-King (35-622)",
"Continental X-King RS (32-622)",
"Continental Terra Trail (40-622)",
],
Schutzblech: ["kein Schutzblech", "SKS Bluemels"],
Schloss: [
"kein Schloss",
"AXA Faltschloss Fold 100",
"Abus Faltschloss Bordo Granit X-Plus 6500/85",
],
Licht: ["kein Lich", "Frontlicht", "Rücklicht", "Front- und Rücklicht"],
},
Hard: {
Größe: ["395 mm", "440 mm", "480 mm"],
Farbe: ["Himmelblau", "Grasgrün", "Stahlgrau", "Weinrot"],
Bremsen: [
"Shimano Deore BRM6000",
"Shimano Deore BRM7000",
"Shimano RX810 1x11",
],
Schaltung: [
"Shimano GRX 600 2x11",
"Shimano GRX 600 2x11",
"Shimano GRX 810 Di2 2x11",
],
Reifen: [
"Continental X-King (35-622)",
"Continental X-King RS (32-622)",
"Continental Terra Trail (40-622)",
],
Schutzblech: ["kein Schutzblech", "SKS Bluemels"],
Schloss: [
"kein Schloss",
"AXA Faltschloss Fold 100",
"Abus Faltschloss Bordo Granit X-Plus 6500/85",
],
Licht: ["kein Lich", "Frontlicht", "Rücklicht", "Front- und Rücklicht"],
}
}
};[/CODE]
[CODE lang="java" title="app.js"]const http = require('http');
const path = require('path');
const express = require('express');
const app = express();
app.use('/', express.static(path.join(__dirname, 'client')));
const server = http.createServer(app);
server.listen(4000, () => {
console.log('Server listening on port 4000');
});[/CODE]
ich übe momentan mit Node.js und express. Ich versuche ein einfachen Fahrrad Konfigurator zu erstellen.
Soweit läuft auch alles. Ich habe eine mock.js Datei erstellt in der sich mein Objekt Array befindet. Dieses exportiere ich nun mit module.exports.
In der bike.js versuche ich nun auf dieses Objekt zu zugreifen, nur funktioniert das leider nicht. Wenn ich jedoch das Objekt direkt in die bike.js schreibe, funktioniert es. Was mache ich falsch. Alle Dateien bis auf die app.js befinden sich in einem Ordner.
[CODE lang="javascript" title="bike,js"]var bikeArray = require('./mock');
console.log(bikeArray.model);
window.onload = function () {
var typSel = document.getElementById("typSel"),
modelSel = document.getElementById("modelSel"),
sizeSel = document.getElementById("sizeSel"),
colorSel = document.getElementById("colorSel"),
breakSel = document.getElementById("breakSel"),
tireSel = document.getElementById("tireSel");
for (var typ in bikeArray) {
typSel.options[typSel.options.length] = new Option(typ, typ);
}
//Give Model
typSel.onchange = function () {
modelSel.length = 1;
sizeSel.length = 1;
if (this.selectedIndex < 1) return;
for (var model in bikeArray[this.value]) {
modelSel.options[modelSel.options.length] = new Option(model, model);
}
};
typSel.onchange();
//Give Size
modelSel.onchange = function () {
sizeSel.length = 1;
colorSel.length = 1;
if (this.selectedIndex < 1) return;
var size = bikeArray[typSel.value][this.value];
console.log(size);
for (var s = 0; s < size.Größe.length; s++) {
sizeSel.options[sizeSel.options.length] = new Option(
size.Größe
size.Größe
);
}
};
//Give Color
sizeSel.onchange = function () {
colorSel.length = 1;
breakSel.length = 1;
if (this.selectedIndex < 1) return;
var color = bikeArray[typSel.value][modelSel.value];
console.log(color);
for (var c = 0; c < color.Farbe.length; c++) {
colorSel.options[colorSel.options.length] = new Option(
color.Farbe[c],
color.Farbe[c]
);
}
};
//Give Breaks
colorSel.onchange = function () {
breakSel.length = 1;
tireSel.length = 1;
if (this.selectedIndex < 1) return;
var breaks = bikeArray[typSel.value][modelSel.value];
console.log(breaks);
for (var b = 0; b < breaks.Bremsen.length; b++) {
breakSel.options[breakSel.options.length] = new Option(
breaks.Bremsen,
breaks.Bremsen
);
}
};
//Give Tires
breakSel.onchange = function () {
tireSel.length = 1;
if (this.selectedIndex < 1) return;
var tires = bikeArray[typSel.value][modelSel.value];
for (var t = 0; t < tires.Reifen.length; t++) {
tireSel.options[tireSel.options.length] = new Option(
tires.Reifen[t],
tires.Reifen[t]
);
}
};
};[/CODE]
[CODE lang="javascript" title="mock.js"]var bikeArray = {
Mountainbike: {
Cross: {
Größe: ["395 mm", "440 mm", "480 mm", "535 mm"],
Farbe: ["Himmelblau", "Grasgrün", "Stahlgrau", "Weinrot"],
Bremsen: [
"Shimano Deore BRM6000",
"Shimano Deore BRM7000",
"Shimano RX810 1x11",
],
Schaltung: [
"Shimano GRX 600 2x11",
"Shimano GRX 600 2x11",
"Shimano GRX 810 Di2 2x11",
],
Reifen: [
"Continental X-King (35-622)",
"Continental X-King RS (32-622)",
"Continental Terra Trail (40-622)",
],
Schutzblech: ["kein Schutzblech", "SKS Bluemels"],
Schloss: [
"kein Schloss",
"AXA Faltschloss Fold 100",
"Abus Faltschloss Bordo Granit X-Plus 6500/85",
],
Licht: ["kein Lich", "Frontlicht", "Rücklicht", "Front- und Rücklicht"],
},
Hard: {
Größe: ["395 mm", "440 mm", "480 mm"],
Farbe: ["Himmelblau", "Grasgrün", "Stahlgrau", "Weinrot"],
Bremsen: [
"Shimano Deore BRM6000",
"Shimano Deore BRM7000",
"Shimano RX810 1x11",
],
Schaltung: [
"Shimano GRX 600 2x11",
"Shimano GRX 600 2x11",
"Shimano GRX 810 Di2 2x11",
],
Reifen: [
"Continental X-King (35-622)",
"Continental X-King RS (32-622)",
"Continental Terra Trail (40-622)",
],
Schutzblech: ["kein Schutzblech", "SKS Bluemels"],
Schloss: [
"kein Schloss",
"AXA Faltschloss Fold 100",
"Abus Faltschloss Bordo Granit X-Plus 6500/85",
],
Licht: ["kein Lich", "Frontlicht", "Rücklicht", "Front- und Rücklicht"],
}
}
};[/CODE]
[CODE lang="java" title="app.js"]const http = require('http');
const path = require('path');
const express = require('express');
const app = express();
app.use('/', express.static(path.join(__dirname, 'client')));
const server = http.createServer(app);
server.listen(4000, () => {
console.log('Server listening on port 4000');
});[/CODE]