0

I want to make a simple server so I can serve local html and JS files while developing.

I tried to get a node app to just take whatever is in the URL, and respond with the page, but no luck (here's my attempt with express).

var fs = require("fs");
var host = "127.0.0.1";
var port = 1337;
var express = require("express");

var app = express();
app.use(app.router); //use both root and other routes below
app.use(express.static('c:\\users\\pete\\projects\\')); //use static files in ROOT/public folder

app.get("/*", function(req, res){ //root dir
  fs.readFile(req.path, function(err,html){
    if(err){
        console.log(err);
        return;
    }
    res.write(html);
    res.end();
  });
});

app.listen(port, host);

but this always looks for the file at c:\, not the correct path.

I've also tried http-server for a simple static server, but it is always crashing when serving js files. https://github.com/nodeapps/http-server

I need to be able to serve your basic html,css,js files simply, and hopefully from any path provide in the URL. This is just for local front-end development. Any help?

Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
pedalpete
  • 21,076
  • 45
  • 128
  • 239

6 Answers6

6

You should give complete path for fs.readFile

fs.readFile('c:\\users\\pete\\projects\\'+req.path, function(err,html){

or you can just do

var host = "127.0.0.1";
var port = 1337;
var express = require("express");

var app = express();
app.use('/', express.static('c:\\users\\pete\\projects\\'));
app.listen(port, host);
vinayr
  • 11,026
  • 3
  • 46
  • 42
1
var host = "127.0.0.1";
var port = 1337;
var express = require("express");

var app = express();
app.use('/', express.static(__dirname));
app.listen(port, host);
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Oct 16 '22 at 10:48
0

If you are intrested in ultra-light http server without any prerequisites you should have a look at: mongoose you can server your files with and it's less than 10MB

yossico
  • 3,421
  • 5
  • 41
  • 76
0

If you are looking for light web server in node which can do:

  • serving static files (static html, js, css, images)
  • executing index.js JS files in node on server side
    (index.js in your document root or in any folder)
  • realoding your index.js apps on change or on error
  • executing any given JS functions
  • optional displaying folder content
  • optional error pages
  • optional handling socket.io packets, session and more

You can check out project Web Development Node.js Server

There are few working demos you can start with:

  • Hello world
    the easiest example, static and dynamic content serving
  • Chat in Angular 1
    static content, dynamic HTTP request, websocket requests, client side in Angular.JS 1
  • Chat in pure JS
    the most complex example, static content, dynamic HTTP request, websocket requests

The server class is possible to extend in standard javascript way,
so you can do anything to customize the behaviour.

The source code is short and easy to understand and it can do mostly
everything, what beginners need with webserver.

Tom Flídr
  • 101
  • 5
0

To get the directory of the templates, I would suggest make use of __dirname and path module of Node.js.

In my case I was in lib directory and templates were a sibling of lib. So below code worked for me to get the templates directory, I'm assuming that you're keeping all your templates in one directory.

let templatesDir = path.join(__dirname,'/../templates/');
cicada_
  • 325
  • 2
  • 9
0

Vinayr is correct. https://stackoverflow.com/a/21082521/7822264

Express' app.use requires a path, and a handler which in this case would be express.static. There is no need to implement any other route.

I would add to his answer that is preferable to use the core module path to build the string paths.

const path = require('path')
path.join(__dirname, 'public') // or whatever is the folder you are storing your static content.

__dirname refers to the directory of the current module.

const port = 1337
const express = require('express')
const path = require('path')

const staticPath = path.join(__dirname, 'static') // for example
const app = express()
app.use('/', express.static(staticPath))
app.listen(port)
Roda
  • 13
  • 6