I’m trying to learn node.js and create a web application, and although I’m doing my best to just use only node.js to create it, I’m having a very difficult time finding resources and examples that describe the process without using other frameworks or libraries.
Specifically, how can I serve HTML and CSS content without using hard-coded string variables, but serving up HTML and CSS files dynamically using just node.js?
Here you’ll build a simple app without using any frameworks. Recommended to get a first touch at node: The Node Beginner Book » A comprehensive Node.js tutorial
The aim of this document is to get you started with developing applications with Node.js, teaching you everything you need to know about “advanced” JavaScript along the way. It goes way beyond your typical “Hello World” tutorial…
This document will probably fit best for readers that have a background similar to my own: experienced with at least one object-oriented language like Ruby, Python, PHP or Java, only little experience with JavaScript, and completely new to Node.js.
Aiming at developers that already have experience with other programming languages means that this document won’t cover really basic stuff like data types, variables, control structures and the likes. You already need to know about these to understand this document.
However, because functions and objects in JavaScript are different from their counterparts in most other languages, these will be explained in more detail…
The URL router implemented there can easily be augmented to serve static files, using the fs
module to read them on disk.
1
From what I understand you are trying to build a static file server on you own.This gist can be a good starting point.It does not give any explanation of the code but it is easy to understand.For instance take a look at this piece of code
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;
Here 4 modules are being required.Now to know more about these modules all you got to do is go the node js api docs and read more about them.Node js ‘s API is very small and this is the best way to learn it.
Also while it can be a very good learning exercise to build a server of your own you should use an already built library for production.