Error with my server file - Invalid URL: index.js - javascript

I am getting the following error when trying to run my server (index.js):
Error: TypeError [ERR_INVALID_URL]: Invalid URL: index.js
The code block is here:
app.get('/:url', async function (req, res) {
try {
return res.status(200).json(data);
} catch (ex) {
console.log(ex);
return res.status(500).json({ message : "Oops." });
}
With the specific line of code it is referring to is:
const site = await .open(decodeURIComponent(req.params.url));
Has anyone ever encountered this error before, and have any idea how to fix it in this context? Not sure how it is throwing an error for my entire index.js server
Note: This is an express app

The value of req.params.url is index.js.
A web browser can expand index.js into an absolute URL because it knows that it can use the URL of the current HTML document as the base URL.
You're running your code in Node.js. It doesn't run inside a document. There is no base URL.
You need to provide an absolute URL (e.g. http://example.com/index.js) yourself.
That said, I suspect wappalyzer may require that you give it the URL to an HTML document not a JS file.

Well, since there isn't much that i can see about your usage, I'm just going to assume you went to something like http://localhost/index.js.
The problem now is, that wappalyzer does not actually get a valid url. It gets index.js without any form of protocol (http/https/ws/wss...). This means it doesn't know what to do and tells you that the url you provided is invalid.
In order to fix this go to http://localhost/https%3A%2F%2Flocalhost%2Findex.js or append https:// or something similar to your parameter.

Related

Error message : MongoServerError: bad auth :Authentication failed

What is the reason behind this error?
this code I am using to connect to DB.
const uri =`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}#cluster0.xft2s.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
Check if you have access to your env contents first, by adding this line console.log(process.env.DB_USER, process.env.DB_PASSWORD).

How to get data from GraphQL CMS

I am trying to follow this tutorial.
I am stuck at 1:02.
Ania is able to generate the thumbnails, but I get an error that says:
TypeError: Cannot read properties of undefined (reading 'thumbnail')
It points to the image tag on the index.js page.
I tried to clear the error that is generated because Next.js wants to use its Image tag instead of img src - but the error still appears. The image block now looks as follows:
I get the same error when I comment the thumbnail line and just try to use title.
I can't make sense of the advice generated when I google this error - such as the note in this post, which I think is suggesting that the code is trying to run before the graphql connection has been made and loaded, for it to be able to retrieve the data. If that understanding is correct, then I think I need to put await somewhere to ask for graphql to load before the results are returned, but I don't know how to do that.
I'm not sure that I've understood the error or advice on how to resolve it correctly.
I have also seen this post, which suggests that problems could be because there are components in the pages folder that are not pages. Is it possible that the [slug].js file being in the pages folder is a source of the problem? If so, where can I move it to solve for that (and why didn't Ania have to do this)?
When I try to console.log randomVideo(videos) beneath the definition of that const (above the return statement), I get two instances of undefined. When I try to console log (randomVideo(videos)).title}, I get the same form of error as posted in the title.
I tried not using the randomVideo const in the return statement, so that the Image tag has:
> <Image
> src={videos.videos[0].thumbnail.url}
> width={400}
> height={400}
> alt={videos.videos[0].title}
> />
When I try this, I get an error that says:
Error: Invalid src prop
(https://media.graphassets.com/NLX6ICjoR5Wt6zYOoLYO) on next/image,
hostname "media.graphassets.com" is not configured under images in
your next.config.js See more info:
https://nextjs.org/docs/messages/next-image-unconfigured-host
I tried amending the next.config.js to the following, but I still get the same error:
/** #type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}
module.exports = {
nextConfig,
images: {
domains: ['assets.example.com'],
},
}
For reference, videos is defined as follows:
const data = await graphQLClient.request(videosQuery)
const videos = data.videos
return {
props: {
videos
}
}
I don't understand why the error messages are different, when I use a different means of getting a video in the return statement.
I have tried console logging all versions of randomVideo inside and outside the return statement, but keep getting undefined.
I've run out of things to try to try and get data in the app.

Discord.js read client bot token from a file rather than hard-coded

I want to be able to read the client.login(BOT_TOKEN); dynamically from a file/database, but this is getting executed before my file read function finishes executing.
BOT_TOKEN = '';
if(BUILD_PROFILE == 'dev'){
filesystem.readFile('./../devToken.txt', 'utf8', (err, data) => {
if(err) throw err;
console.log(data);
BOT_TOKEN = data;
})
}
client.login(BOT_TOKEN);
This is the error I'm getting in logs - I have double checked the file and it's console.log(data) shows the right token, but it's not being applied
I suggest you place your token in an ENV file.
I also think you should copy your token directly from your bot's bot page on discord and pasting it directly.
You console.log'd the data was it the right token?
A very easy way to do this would be to have a config.js file in your main bot folder, and set out the
{
token: “token-here”
}
Then, in your main.js file, require the config file as a variable, then at your ‘bot.login’, just do ‘bot.login(config.token)’
You can also have your prefix set in this file too, allowing a user to possibly change your command prefix in the future
Additionally, you could use a SQLite database, that saves your token - you have to have the SQLite npm library, from https://www.npmjs.com/package/sqlite here, but it is very simple to set up, if anyone needs help here, add my discord Proto#4992
n.m. SQLite databases also will come in useful when/if you want to set up a currency system in the future.

Uploading file Angular.js FormData

I'm uploading a File with Angular to my Node server. Sometimes the image is not uploaded and I get req.files "undefined" in the server side and I can not figure it out why. I think it has not to do with the weight of the image: the image that is uploaded is around 48KB and the one that isn't 16KB. I attach two snapshot to show the difference in timing (http post) between the one that works and the other (network tab of Chrome developer tool). I can see that the "times" ("blocking", "sending") are not overlapping themselves in the one that is failing. Maybe is that the problem... but I'm not sure about how to fix it.
I got it.
I was using express.multipart in the server side with defer option "express.multipart({defer: true})"
Because of that, sometimes the execution reached the code where I manage the post request to get the image with "req.files.file" before form's "end" event in express.multipart module is fired where the module sets req.files:
form.on('end', function(){
if (done) return;
try {
req.body = qs.parse(data);
**req.files = qs.parse(files);**
if (!options.defer) next();
} catch (err) {
form.emit('error', err);
}
});
So sometimes req.files.file = undefined, simply because is not set yet...
Getting rid of {defer: true} option solves my problem. Nothing to do with one or another image.
Here you are the doc of "defers" option in express.multipart module:
"defers processing and exposes the Formidable form object as req.form.
* next() is called without waiting for the form's "end" event.
* This option is useful if you need to bind to the "progress" event, for example."

Node.js Express.js | Automatically render handelbar templates from views/static folder

I have got the following code to automatically load my handelbar templates from the views/static folder without having to manually setup a route for each page.
app.get("/:template", function(req,res){
var template = req.params.template; // Is this safe?
res.render("static/" + template, function(err, html) {
if (err) {
res.send(404, 'Sorry cant find that!');
} else {
res.send(html);
}
});
});
It works fine, however I am worried that this potentially exposes my app to security problems. Any suggestions how I could do this better. I am using Express. Thanks so much for your help.
I think it's pretty safe.
Usually, you have to worry about paths being passed that contain stuff like ../ (to go back a directory level), but those won't match your route. Also, the route you declare will stop matching at a /, so requests like /foo/../bar won't match either.
An issue that may occur is when the static directory contains files that you don't want to expose: a request for /secret.js will at least try to render a file called static/secret.js.

Categories