I would like to make at least a few simple express.js examples just for the sake of making a few actual projects with the framework. There is learning the basics of working with express, or any framework for that matter, but sooner or later I have to make a few actual projects with it or move on to something else.
This express.js example aims to just be a simple little projects that is not intended to be used in production, but just a simple silly little thing to have up and running on a local network for a little while. The idea here is to make use of nodejs, express, ejs, and express session, to just have a simple little canvas powered client system with little pointer circles in it. Each circle in the canvas is a client currently connected to the server on the local network.
I am going to be keeping this example simple in the sense that I am not going to be doing anything fancy with authentication, making use of a database and so forth. When it comes to session storage I am just going to be using the default memory store. However I think that doing so is okay sense I intend for this to just be a simple little thing that will only have a few computers connected to it at any given time, and it is just going to be a simple little think where there is just serves as a crude starting point for something that will end up getting very complicated when it comes to making this into some kind of real project.
1 - The public folder
First off I will be starting with the public folder that will be at the root of the project folder. In here I have a javaScript folder that will contain a few javaScript files that will compose the client system.
1.1 - The /public/js/http.js file that will be my simple http client
I have an http.js file that will serve as my custom http client for this project.
I have a utils.js file that contains some methods that I will be using to create a canvas element, and preform some other basic tasks for my main client system.
var setName = document.getElementById('set_name');
setName.value = infoObj.name;
setName.addEventListener('change', function(e){
infoObj.name = e.target.value;
var save = {
name: infoObj.name
};
utils.save('network_pointers', 0, save);
});
1.4 - The /public/style.css
I also have an extrela css file in my pubmic folder.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
body{
background: white;
}
.wrap_main{
padding: 10px;
background: #2a2a2a;
color: #ffffff;
margin-left:auto;
margin-right:auto;
width:90%;
}
.wrap_header{
padding: 10px;
text-align:center;
min-height:150px;
background:#5a5a5a;
}
.wrap_content{
margin-top:10px;
padding: 10px;
text-align:center;
min-height:150px;
background:#8a8a8a;
}
2 - The ejs files
I am using ejs for server side rendering of html in this example.
2.1 - index.ejs
Here I have my main index.ejs file that I will be calling with app.render in the main app.js file. Each time that I do so I will be passing any object that will contain the current layout to use for the page. For now there is just one layout for a main client system, and another for a 404 page. However if I do get around to working on this a little longer there might be additional layouts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!doctype html>
<html lang="en">
<head>
<title>Express EJS and static file hosting.</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/style.css">
<script src="/js/utils.js"></script>
<script src="/js/http.js"></script>
</head>
<body>
<div class="wrap_main">
<div class="wrap_header">
<h1>EXpress EJS Demo</h1>
</div>
<div class="wrap_content">
<%- include('layouts/'+ layout +'.ejs') %>
</div>
</div>
</body>
2.2 - home
1
2
3
4
5
<h1>Client</h1>
<input id="set_name" type="text" ><br><br>
<div id="canvas-app"></div>
<script src="/js/client.js"></script>
2.3 - 404
1
2
<h1>404 Page not found</h1>
<p>The page was not found</p>
3 - The main app.js file
Now I have my main app.js file at the root of the project folder, this is the file that will be called with nodejs to start the server.
console.log('app is up on port: ' + app.get('port'));
});
4 - Conclusion
I had a very general ides of what it is that I wanted for this simple express.js examples, and all ready this is more or less what I had in mind. I was able to throw it together in a falsh, but there is just a little more I might add to this, so I can not say that there is much more to do with this.
I just wanted a simple express.js example that serves as a starting point examples for session storage and what can be done with it. When it comes to the database, storage data and so forth that is all just stored in memory. A real project that is worthy to be deployed, and used by a lot of people would involve so much more than this. There would need to be authentication, a user database, a proper store for session data, and so much more. Not to mention all the work that would need to be done when it comes to sever side sanitation, and so forth, and so on. However the crude basic idea of what is possible with nodejs, and express is there, just at a crude micro level, and making a real project is not waht I had in mind.