How to use the Server function from node-static

Find comprehensive JavaScript node-static.Server code examples handpicked from public code repositorys.

node-static.Server is a Node.js module that serves static files such as HTML, CSS, JavaScript, and images over HTTP.

53
54
55
56
57
58
59
60
61
62
    // devtools: true,
}).then(
    async (browser) => {
        const server = http.createServer(handler);
        const localServer = http.createServer(handler);
        const staticFileServer = new nodeStatic.Server('./public');
        const dbClient = new MongoClient(process.env.MONGODB_URI);
        const database = dbClient.db(process.env.MONGODB_DB_NAME);
        const downloadEvents = database.collection('download-events');
        let port = 3001;
fork icon8
star icon11
watch icon6

+ 3 other calls in file

949
950
951
952
953
954
955
956
957
958
    responseStatus = 404;
    responseMessage = Configuration.getStringTableEntry('Resource not found', {url: action});
} else if (configuration.staticFilePath != null) {
    // serve static assets requests from the local folder.
    if (staticFileServer == null) {
        staticFileServer = new nodeStatic.Server(configuration.staticFilePath);
    }
    if (staticFileServer != null) {
        staticFileServer.serve(request, response);
    }
fork icon4
star icon10
watch icon4

+ 8 other calls in file

How does node-static.Server work?

When you create a new instance of node-static.Server in your Node.js code, you specify a directory where your static files are stored. When a client makes a request for a file, the server checks if the file exists in the specified directory. If the file exists, the server sets the appropriate MIME type for the file and sends it back to the client as the response to the request. If the file doesn't exist, the server sends a "404 Not Found" error response to the client. The server also handles other HTTP methods such as POST, PUT, and DELETE by sending "405 Method Not Allowed" responses. The server can also handle Range requests for large files, which enables clients to request only a portion of the file, making it possible to download large files more efficiently. Node-static.Server also supports a caching mechanism to improve performance by caching frequently requested files in memory. When a client requests a file that has been cached, the server sends the cached version of the file instead of reading it from disk again. Overall, node-static.Server simplifies the task of serving static files in Node.js by handling many of the details of the HTTP protocol and file system interactions for you.

31
32
33
34
35
36
37
38
39
40
41
//UDP
udpserver.bind(9614);


function handler(req, res)
{
    var file = new node_static.Server(path.join(__dirname, '/web/'), {cache: 1});
    file.serve(req, res, function(err, result)
    {
        res.writeHead(404);
        return res.end('File not found');
fork icon1
star icon1
watch icon2

+ 12 other calls in file

3
4
5
6
7
8
9
10
11
12
13
14
15
const http = require("http");
const static = require("node-static");


const port = 8888;


const staticFiles = new static.Server(`${__dirname}/test-sites/`);


const httpServer = http.createServer((req, res) => {
  if (req.method == "GET") {
    // Every GET request is simply served as static file.
fork icon0
star icon2
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const static = require("node-static");

// Create a new instance of node-static.Server
const fileServer = new static.Server("./public");

// Serve the files in the 'public' directory over HTTP
require("http")
  .createServer(function (request, response) {
    request
      .addListener("end", function () {
        // Serve files using node-static.Server
        fileServer.serve(request, response);
      })
      .resume();
  })
  .listen(8080);

In this example, we first require the node-static module and create a new instance of node-static.Server, specifying the directory where our static files are stored (./public). Next, we create an HTTP server using Node.js' built-in http module, which listens for incoming requests on port 8080. When a request is received, we use fileServer.serve to serve the requested file using node-static.Server. The addListener method is used to ensure that the request body is fully consumed before attempting to serve the file. Finally, we call the listen method on the HTTP server to start listening for incoming requests. When we run this code, any files in the ./public directory will be served over HTTP on port 8080.

10
11
12
13
14
15
16
17
18
19
20
21
const readline = require("readline");


try {
db.init(function (conf) {
    //Not specifying any header options here, default caching is one hour.
    var fileserver = new nodestatic.Server(path.join(db.appdir(), "docroot"));
    var websrv = {};


    function params2Obj (str) {
        var po = {};
fork icon0
star icon1
watch icon1

+ 9 other calls in file

13
14
15
16
17
18
19
20
21
22
23
24
    port = 8080;
     directory = './public';
}


/* Set up static file web server to deliver files from the file system */
let file = new static.Server(directory);


let app = http.createServer(
    function(request, response) {
        request.addListener('end',
fork icon0
star icon1
watch icon2

+ 9 other calls in file

101
102
103
104
105
106
107
108
109
110
111
112
  });
}




function startStaticServer(port) {
  const fileServer = new statics.Server(`${__dirname}/data`);
  http.Server((req, res) => {
    req.addListener('end', () => {
      fileServer.serve(req, res);
    }).resume();
fork icon0
star icon0
watch icon1

+ 4 other calls in file

16
17
18
19
20
21
22
23
24
25
 * Starts the GoAPI server.
 * @returns {void}
 */
module.exports = function () {
	const server = new httpz.Server();
	const file = new static.Server(path.join(__dirname, "../server"), { cache: 2 });
	
	server
		// add middlewares
		.add(reqBody)
fork icon0
star icon0
watch icon1

+ 8 other calls in file

4
5
6
7
8
9
10
11
12
13
14
15
const { v4: uuid } = require('uuid');
const dogNames = require('dog-names');
const shuffle = require('knuth-shuffle-seeded');
const { getScore } = require('./score');


const fileServer = new staticServer.Server(path.join(__dirname, '../../build'));


const sockjsOpts = {
  sockjs_url: 'https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js',
  prefix: '/api',
fork icon0
star icon0
watch icon1

+ 4 other calls in file