Node.js -Express

Hey guys I am making a website and I want to do something similar to the login buttons for this website. My plan is to have some client side javascript that will toggle the Sclasses for an image and for the login and create account button and let the css hide them. I was wondering how I would get the image from my database to the client?

The site has a Node.js server, MySQL Database, for the client I am using Express.

1 Like

How are you storing the image?
Is it on a filesystem with the database storing the location or is it stored as bytes in the database?

I have never stored a image in a database before so I wanted to give it a shot. Essentially I want to store it as bytes as a text field but I am not sure on how to process those bytes and get that to the client.

What you will want to do is to store it in a format that is specifically designed for storing raw binary data (maybe blob?)

To transmit it to the client, you can either just dish out the image in a HTTP response, you can create your own protocol to transfer the data or you can use another protocol.

When you say create my own protocol do you mean with JSON? Could I see a general example of creating a protocol and sending and receiving?

If you wanted to create your protocol, you would need to create a standard format for sending data.

So your client and server could both communicate.
Regardless of the data, there must me an agreed upon format that it will be sent as for it to work.

I would suggest using HTTP to serve the image, and just have the client display that

Is that done through AJAX?

Ajax is a method of requesting data through the HTTP protocol

You can use that

Thanks, I am brand new to this so still learning the basics.

Also I want to be able to do an if then statement to check if the account is logged in or not. If I had a java script file on the server could that hold variables while the client is in session so I could make an AJAX call to the server to get those variables?

For something like this, the client should be nothing more then an interface to display data given by the server, and give data to the server.

Following this principle. You should not be performing any security checks in the client.

If you want the client to know if it's logged in. Get it to check if it's got a session token, or ask the server to give it a JSON response telling it if it is or not.

Sorry I didnt mean on the client. I am using node.js so its all javascript for the server as well. I meant if I create a variable in the server code is that specific to the session?

There are many ways, the easiest and probably most effective (although not the most optimised) solution is a table in a database.

Variables would be kept there with a field which would relate them to a specific session.

You could select all the variables that have the same session token as this user, and then you're done.

If you are using node.js I assume you are also using a web library, if so it should have functions for this.

I think I will try something called Redis for this.

Storing file data in a db is generally a bad idea. Relational databases are not meant to store such large chunks of data and you'll notice performance issues pretty fast if you're making a real world app. The only exception that I know of is Postresql's large object store which breaks your large chunks of data into multiple rows to keep performance decent.

You could do the same manually for mysql. Split your images into multiple rows in a blob column.

I would have no idea how to do that lol. Would it work then just to store it in the directory structure of the server?

Yea that would work. I'd recommend looking into the fs module.

Also, for handling file upload streams in express: https://github.com/expressjs/multer

I will look into that in a moment. For now I need a way of holding session data for each client and I assume I somehow use something called a cookie as a unique key to find data unique to each user. In this case I want to use Redis but I am not sure how to do it in code.

An example would be to see if the person is logged in or not.

You can just store the login token in the database. e.g.,

create table session_token(
  token text,
  user_id text
);

create table user(
  id text,
  name text
);

Then on every request you can just:

select user.* from user
inner join session_token
  on user.id = session_token.user_id and session_token.token = ?

To pull down the user data for the logged in user. You can add a timestamp to the session_token table to make logins expire if you want later on.

Does this code automatically generate a session cookie for each client?

app.use(express.cookieParser());
app.use(express.session({
  secret: 'unguessable',
  store: new RedisStore({
    client: redisClient
  })
}));