Express example of serve index middleware

When working out a nodejs project it would be nice to have a way to just quickly create something that will just serve an index of a public html folder and that is it. I could take the time to work out my own solution when it comes to that, but even simple things like this can often prove to be a little time consuming. If I am willing to make expressjs part of the stack, and often that is one npm package that I do not mind using, then there is a middleware for express called serve index that can make quick work of this kind of task.

The serve index middleware can be combined with the built in express static function as a way to serve an index for a path, while serving up files when a full path with file name is given. So then bu just using express and one additional package I can quickly have a system that will work well as a way to serve up and index of files for a path of files some of which might be html files. In addition when a user clicks an html file all the assets will load thanks to the built in express static function.

So then in this post I will be going over a quick simple expressjs example that will involve using nodejs, express, and serve-index to create a simple static server that will host a public folder.

Read More

Using The Buffer slice method in nodejs

When it comes to working with buffers in nodejs there is the nodejs buffer slice method that works more or less just like the Array slice method that will create a new array from an array without mutating the source array.

The slice method is often what I would use when it comes to creating a sub array from an array because of the nature of not mutating the array in place. There is however the array splice method that will mutate the array in place, so then the question might arise as to how to go about doing that with a buffer in nodejs. When looking at the nodejs docs it would seem that there is no such method in the Buffer class, however there is the Buffer write method. When it comes to the write method that is what can be used to mutate a buffer in place, much like that of the Array splice method.

So in this post I will be going over a few quick examples of the Buffer slice method. However in the process of doing so I guess I might also want to go over a few more examples of the write method when it comes to mutating a buffer in place, and other ways to go about creating a new buffer from a buffer.

Read More

Using process.stdout in place of console.log

In some cases I might want to use process.stdout in place of console.log when working out a nodejs script. The console.log method works just fine for most typical user case examples, however it does append a line feed at the end of the output each time. Often this might be what I want to happen, however when it comes to having better control over the standard output of a script the write method of the strout stream in the process global is how to go about doing so.

Read More

The current working dir and other dirs of interest in Nodejs with Process.cwd()

One thing that I wish that I got solid early on when I was first starting out with nodejs is how to get the current working directory, and also how to always get the directory where a script is located, along with other typical paths of interest on a system. In the process global there is a cwd method that when called will return the current working directory, which is of course a major directory of interest when it comes to creating a nodejs script. However it is not the only directory of interest of course it is also important to know how to go about getting the directory of the current script, and also how to get paths to assets that are relative to that script. There is also how to go about getting the user folder location when it comes to a standard location to park user specific data.

So in this post I will be covering a basic example of the process.cwd method of course, but I will also be touching base on a whole bunch of other little topics that revolve around that.

Read More

Nodejs Examples

I have wrote a lot of posts on nodejs thus far, and I have covered all the basics I think at this point. There are many things that I like to write about, but at this post I think that when it comes to nodejs from this point forward I should just write about some actual nodejs examples. When I do something to this effect with a topic like node I often write a post that will serve as a post where I will link to everything that I have done thus far when it comes to some actual examples that make use of a language, framework, or something to that effect. As such this post will be on the collection of nodejs examples that I have made thus far.

As of this writing I can not say that I have made anything that is really something to be proud of yet. In time that might change, but yet again maybe not as there are many other areas of interest when it comes to javaScript. It would seem that I am more interested in client side javaScript at the time of this writing at least, because these days I seem more interested in working on my canvas examples. However at some point in the future that might change, and when that happens I will end up treating this post the same way as my post on canvas examples. In other words I will keep coming back to it, and edit and expand the post with additional links to all the examples I have made thus far.

Read More