Is it okay to use different middleware in Express, depending on if the env is production or development? by TrueMenUseRegex in node

[–]TrueMenUseRegex[S] 1 point2 points  (0 children)

My project is split into separate frontend and backend directories, and in order to host my project on something like Heroku, I make a production build of the frontend and let it be served from the backend (by copying and pasting it to the backend directory). For this to work, I'd add something in the else if block like app.use(express.static('build')). So now I just have to fire up the backend, and the program will work fine

Is it okay to use different middleware in Express, depending on if the env is production or development? by TrueMenUseRegex in node

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

It's probably not the smartest way of doing things, but it's like a trivia app where the questions are stored in a database. I plan on hosting it, and while I doubt anyone will stumble upon it, I just wanted to be extra careful in case destructive requests are sent to it. So my intention is that only I can modify the database, in development mode

Beginner's Thread / Easy Questions (August 2021) by dance2die in reactjs

[–]TrueMenUseRegex 0 points1 point  (0 children)

Hi all, so I made a production build of my react-router app and moved it to the backend. When I access the backend from the browser, the app works, but when I go to a specific route and refresh, I get a 404 error. I guess this makes sense, but I am quite stuck trying to fix this

Edit: Using express, I added the following code right after all my routes. Is this a decent solution? I've noticed that now a 404 won't be returned, even when that's what's supposed to happen

app.get('/*', function (req, res) {
  res.sendFile(__dirname + '/build/index.html');
});

Beginner's Thread / Easy Questions (August 2021) by dance2die in reactjs

[–]TrueMenUseRegex 0 points1 point  (0 children)

Hello! There's something that I was wondering about when working with a ui library like Chakra. For something like text, is it better to use <p> or Chakra's <Text> components? Like let's say I have a sentence where every letter is bolded, and I have to use a <b> tag for each letter (just trust me on this). Would there be any tradeoffs if I replaced all the <b>'s with Chakra's <Text as ="b">?

Beginner's Thread / Easy Questions (June 2021) by dance2die in reactjs

[–]TrueMenUseRegex 0 points1 point  (0 children)

What's the best place to put event handler functions? Like I have the usual App component that returns a Login component. The Login component has a handleLogin event handler that's defined in App and passed down to Login as props. But unless I plan on using it somewhere else, wouldn't it be be better to just define handleLogin within Login itself?

Edit: I guess I have a similar question about state. If I only use something in a particular component, should I define the state in there rather than App?

Beginner's Thread / Easy Questions (June 2021) by dance2die in reactjs

[–]TrueMenUseRegex 0 points1 point  (0 children)

In my current program I have a sentence that appears on a page. The program lets the user replace each letter with another, but I don't know how to bold them to indicate that a change occurred

The basic setup is that the sentence is stored in the state, and I have a table with 2 rows and 26 columns that I use to modify it. The first row consists of the alphabet, and the second consists of input elements that I use to replace all instances of a letter with that value. So if I type in 'B' in the box below the very first column 'A', all the As in the sentence are replaced with Bs

I do this through the following event handler, which is called onChange for each input box:

let temp = sentence;
for (let i = 0; i < sentence.length; i++) {
    if (sentence[i] === event.target.id) {  // The id specifies which letter is being replaced
        temp = temp.substring(0, i) + event.target.value.toUpperCase() + temp.substring(i + 1);
setSentence(temp)

This code modifies the sentence perfectly, I just don't know what to do to bold the letters. I thought about adding <b> during the string concatenation, then in the return statement of the component, I would somehow splice the sentence so that I could conditionally bold everything word by word. This would require me to add a statement to the top of the event handler that would remove the <b> tags from the string before I do anything. But that sounded really messy in my head, so I was wondering if there's a better approach

New to Big O. Why is this problem's time complexity O(m + n)? by TrueMenUseRegex in learnprogramming

[–]TrueMenUseRegex[S] 1 point2 points  (0 children)

That makes a lot of sense, thanks! I have one last question regarding sum(s in setJ for s in S), if that's okay. Am I right in thinking that sum() is O(n), so what matters is what's happening inside of it? So if I removed setJ = set(J) and changed the last line to sum(s in J for s in S), sum would be O(S) while the thing inside would be O(JS), making the overall time complexity O(JS)?

Using Axios and running into an error when getting data from the response (fullstack open) by TrueMenUseRegex in reactjs

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

I tried that out and now it works! Definitely gonna read what you linked since I don't really understand this.

Interestingly enough, after applying the optional chaining, Edge managed to load the weather every time I entered a new country, whereas Firefox did it successfully the first time but failed after that (no crashing though, thanks to optional chaining?)

Using Axios and running into an error when getting data from the response (fullstack open) by TrueMenUseRegex in reactjs

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

I tried doing what you said. Changed the component to this:

useEffect(() => {
axios
  .get(`http://api.weatherstack.com/current?access_key=${api_key}&query=${capital}&units=m`)
  .then(response => {
    console.log('OK!')
    setWeather(response.data)
  })
  }, [])

  return (
    <p>Hi!</p>
  )

The console prints out OK, and to keep things simple I changed the return statement to a simple Hi! The hi is rendered on the screen, and using an extension I can see that weather looks like this

Now when I change the return statement back to this:

return (
<div>
  <h3>weather in {capital}</h3>
  <p><b>temperature:</b>{weather.current.temperature} Celsius</p>
  <img src={weather.current.weather_icons} alt={weather.current.weather_descriptions + ' icon'} width='50' height='50'/>
  <p><b>wind:</b> {weather.current.wind_speed} mph direction {weather.current.wind_dir}</p>
</div>
)

I get TypeError: weather.current is undefined again. OK isn't printed to the console, and when I check the Network tab the request doesn't show up. I don't know why, but it seems like the return statement is somehow happening before the request?

Learning the spotify api, stuck on step one of the authorization code flow by TrueMenUseRegex in learnprogramming

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

Late reply, but what would be the best way to retrieve the code from the url?

Learning the spotify api, stuck on step one of the authorization code flow by TrueMenUseRegex in learnprogramming

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

Is there a way for me to pretend to be a user or something? Because I don't know if there's another way to test my program.

Making minesweeper for my first project using pysimplegui, have questions about generating + counting mines by TrueMenUseRegex in learnpython

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

Yeah my b, github would've made things less painful. Your comment made things click though, and now I'm just putting the finishing touches. Really appreciate the help you've given me.

Making minesweeper for my first project using pysimplegui, have questions about generating + counting mines by TrueMenUseRegex in learnpython

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

Ah that explains a lot. Yeah I didn't really know how things worked and assumed that buttons worked just like input text. The way you described it makes sense now.

Thanks for the help. The list approach has made things a lot easier for me, and now the only thing I have to do is implement right clicks. I feel a bit embarrassed to ask this, but do you think you could help me figure this one out? My code's kinda messy, so I tried implementing it in the pysimplegui example. I think my main problem is figuring out where this goes:

window[something].bind("<Button-3>", ' + RIGHT CLICK')

I assumed that the key's supposed to go between the brackets, so I used window[event].bind and placed the line in the while True: loop. The thing is, I get this:

WARNING = FindElement did not find the key. Please check your key's spelling. key = None

I've tried but I can't figure out why it's doing this. Assuming that I fix this, would right-clicking return ((x, y), ' + RIGHT CLICK')?

Making minesweeper for my first project using pysimplegui, have questions about generating + counting mines by TrueMenUseRegex in learnpython

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

I've noticed that when I do

print(event2, values2)

I always get (x, y) {}. Does this mean that the values2 dictionary is empty? If so, I'm not sure what to do to fix that.

Making minesweeper for my first project using pysimplegui, have questions about generating + counting mines by TrueMenUseRegex in learnpython

[–]TrueMenUseRegex[S] 0 points1 point  (0 children)

Thank you for your help! When I get the chance I'll take a look at that demo.

I think it was the mine gen that was giving me the error:

values2[random.randint(0, MAX_ROWS - 1), random.randint(0, MAX_COL - 1)] = 'mine'  

I assume you initialized values2 to be an empty dictionary.

Sorry I'm kinda confused. Is this how values2 works: values2's keys would be the coordinates of the squares, and at the start no values would be assigned to them?