I'm making some frontend experiments and I'd like to have a very basic webserver to quickly start a project and serve the files (one index.html file + some css/js/img files). So I'm trying to make something with node.js and express, I played with both already, but I don't want to use a render engine this time since I'll have only a single static file, with this code I get the html file but not the assets (error 404):
var express = require('express'), app = express.createServer(); app.configure(function(){ app.use(express.static(__dirname + '/static')); }); app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); app.listen(3000);
Is there a simple way to do it (in one file if possible) or Express requires the use of a view and render engine ?
The open source text editor Brackets also includes a NodeJS static web server. Just open any HTML file in Brackets, press "Live Preview" and it starts a static server and opens your browser at the page. The browser will auto refresh whenever you edit and save the HTML file.
The Node. js framework can be used to develop web servers using the 'http' module. The application can be made to listen on a particular port and send a response to the client whenever a request is made to the application.
You can use IIS or Apache to run Node. js web application but it is recommended to use Node. js web server.
Delivering HTML files using Express can be useful when you need a solution for serving static pages.
I came across this because I have a similar situation. I don't need or like templates. Anything you put in the public/ directory under express gets served as static content (Just like Apache). So I placed my index.html there and used sendfile to handle requests with no file (eg: GET http://mysite/):
app.get('/', function(req,res) { res.sendfile('public/index.html'); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With