The router in my react app throws an error - reactjs

This is the error:
throw new TypeError('app.use() requires a middleware function')
^
TypeError: app.use() requires a middleware function
at Function.use (/Users/Andres/Desktop/chat-app-2/server/node_modules/express/lib/application.js:210:11)
at Object.<anonymous> (/Users/Andres/Desktop/chat-app-2/server/index.js:13:5)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...
ROUTER.JS
const express = require('express');
const router = express.Router();
router.get(`/`, (req, res) => {
res.send('server is up and running');
});
module.export = router;
INDEX.JS
const express = require('express');
const socketio = require('socket.io');
const http = require('http');
const PORT = process.env.PORT || 5000;
const router = require('./router');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.use(router);
server.listen(PORT, () => console.log(`Server has started on port ${PORT}`));
Can someone help me with these?

According to https://expressjs.com/en/guide/routing.html you need to provide a route where the imported router is support to run. Try:
app.use('/', router);
or you can use a different route, like /mysubroute.

Related

CLIENT_MISSING_INTENTS with d.js

const Discord = require('discord.js');
const bot = new Discord.Client();
const client = new Client({
disableMentions: "everyone",
ws: {
intents: ["GUILDS", "GUILD_MEMBERS", "GUILD_MESSAGES", "GUILD_PRESENCES"]
}
})
const token = 'my token';
bot.on('ready', () =>{
console.log('This bot is online!')
})
bot.login(token)
when I run "node ." this results in this error. I don't really understand it as I added intents?
C:\Users\User\OneDrive\Desktop\Discord bot\node_modules\discord.js\src\client\Client.js:544
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (C:\Users\User\OneDrive\Desktop\Discord bot\node_modules\discord.js\src\client\Client.js:544:13)
at new Client (C:\Users\User\OneDrive\Desktop\Discord bot\node_modules\discord.js\src\client\Client.js:73:10)
at Object.<anonymous> (C:\Users\User\OneDrive\Desktop\Discord bot\index.js:2:13)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
[Symbol(code)]: 'CLIENT_MISSING_INTENTS'
}
I'm new to this so please be kind when you find my obvious flaw.
Thanks in advance!
Intents is new thing that came as requirement in v13 now you have few issues in your intents but that is great start. You would need to define intents following way:
const { Client, Intents } = require("discord.js");
client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_PRESENCES]
});

How do I fix ReferenceError: command is not defined

I'm trying to make a discord bot and I get the error "ReferenceError: command is not defined" I don't understand why, can someone help?
heres my code:
console.clear();
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ['GUILDS', 'GUILD_MESSAGES']});
const Client = require("./Structures/Client.js");
const config = require("./Data/config.json");
client.commands = new Discord.Collection();
const fs = require("fs");
fs.readdirSync("./src/Commands")
.filter((file) => file.endsWith(".js"))
.forEach((file) => {
/**
* #type {Command}
*/
const commands = require(`./Commands/${file}`);
console.log(`Command ${command.name} loaded`);
client.commands.set(command.name, command);
});
client.on("ready", () => console.log("SpidBot is online!"));
client.on("messageCreate", (message) => {
//console.log(message.content);
//if (message.content == "!Spid") message.reply("Hello!");
if (!message.content.startsWith(config.prefix)) return;
const args = message.content.substring(config.prefix.length).split(/ +/);
const command = client.commands.find((cmd) => cmd.name == args[0]);
});
client.login(config.token);
heres the error:
C:\Users\Aly\Desktop\SpidBot\src\index.js:22
console.log(`Command ${command.name} loaded`);
^
ReferenceError: command is not defined
at C:\Users\Aly\Desktop\SpidBot\src\index.js:22:26
at Array.forEach (<anonymous>)
at Object.<anonymous> (C:\Users\Aly\Desktop\SpidBot\src\index.js:17:3)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47
It appears theres some more errors, so can someone help me fix it?
You accidentally misspelt the variable name on line 21:
const commands = require(`./Commands/${file}`);
You named the variable "commands". Simply remove the 's' and it should work.
So it should be:
const command = require(`./Commands/${file}`);

React-Native Firebase Deploy Error command Cli

Hi everyone i try to pass some images to my firebase with Functions but when i deploy firebase(with cli firebase deploy)inside my terminal i got an error and the deploy abort.
Apparently the error say my require inside function/index.js is not a function (line 9 col 45) but the gcconfig was do with the right method for me.
I want to correctly deploy firebase from CLI.
The error from cli firebase deploy i got on my terminal:
Error: Error occurred while parsing your function triggers.
TypeError: require(...) is not a function
at Object.<anonymous> (/Users/luca/Code/Code_Taff/Benibla/bnblproto/functions/index.js:9:45)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
at Module.require (internal/modules/cjs/loader.js:643:17)
at require (internal/modules/cjs/helpers.js:22:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15
at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)
function/index.js:
const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
const fs = require('fs');
const UUID = require("uuid-v4");
const gcconfig = {
projectId: "beniblaproto",
keyFilename: "beniblaproto.json"
};
const gcs = require("#google-cloud/storage")(gcconfig);
exports.storeImage = functions.https.onRequest((request, response) => {
return cors(request, response, () => {
const body = JSON.parse(request.body);
fs.writeFileSync("/tmp/uploaded-image.jpg", body.image, "base64", err => {
console.log(err);
return response.status(500).json({error: err});
});
const bucket = gcs.bucket("beniblaproto.appspot.com");
const uuid = UUID();
return bucket.upload(
"/tmp/uploaded-image.jpg",
{
uploadType: "media",
destination: "/events/" + uuid + ".jpg",
metadata: {
metadata: {
contentType: "image.jpg",
firebaseStorageDownloadTokens: uuid
}
}
},
(err, file) => {
if (!err) {
response.status(201).json({
imageUrl: "https://firebasestorage.googleapis.con/v0/b" +
bucket.name +
"/o/" +
encodeURIComponent(file.name) +
"?alt=media&token=" +
uuid
});
} else {
console.log(err);
response.status(500).json({error: err});
}
}
);
});
});
if someone can explain me where and for what i fail.
Thank you all have a good day
Firstly you need to import {Storage} of library;
const {Storage} = require("#google-cloud/storage");
after that you need to take an instance with your configuration object;
const gcs = new Storage(gcconfig);

socket.io react heroku 405

I have the following server file:
const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = module.exports.io = require('socket.io')(server)
const PORT = process.env.PORT || 3231
const SocketManager = require('./SocketManager')
app.use(express.static(__dirname + '/../../build'))
io.on('connection', SocketManager)
server.listen(PORT, ()=>{
console.log("Connected to port:" + PORT);
})
And I am connecting it on the front end like so:
import React, { Component } from 'react';
import io from 'socket.io-client';
import Game from '../Game/Game';
import axios from 'axios'
const socketUrl = "/"
const socket = io(socketUrl)
But I keep getting this in my console:
Failed to load resource: the server responded with a status of 405 (Not Allowed)
I really am not sure what to change or set up to rectify what is an error.
My set up was not proper for the people that are having trouble with socketio connection with their backend from a react frontend. This worked for me:
const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = module.exports.io = require('socket.io')(server, { origins: '*:*'})
const path = require('path')
// for prod dont use port #
const PORT = process.env.PORT || 3001
const SocketManager = require('./socketmanager')
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
io.on('connection', SocketManager)
server.listen(PORT, ()=>{
console.log("Connected to port:" + PORT);
})

Koa start error

......\node_modules\koa\lib\response.js:47
const { res } = this;
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (......\node_modules\koa\lib\application.js:11:18)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
When I start my koa demo, console shows this.
node version: v4.4.7
"koa": "^2.2.0"
Here is my entry.js:
const Koa = require('koa');
const app = new Koa();
app.use(ctx => {
ctx.body = 'Hello Koa';
});
app.listen(3000);
After I remove {} in response.js:47 and context.js:124
const res = this;
everything runs ok.I don't know why.
Because const res = this; mean just constant variable res equals this but
const { res } = this; is object destructuring (http://exploringjs.com/es6/ch_destructuring.html) it's equal const res = this.res; So if this has not property res you'll receive error.

Resources