📜  如何在 Node.js 中使用带有服务静态文件的路由?

📅  最后修改于: 2022-05-13 01:56:31.666000             🧑  作者: Mango

如何在 Node.js 中使用带有服务静态文件的路由?

本文的目的是学习如何在 Node.js 中使用路由来服务静态文件,它有一个内置的 HTTP 模块来通过超文本传输协议 (HTTP) 传输数据,该协议支持以前难以使用的各种功能。在此模块提供的功能的帮助下,从子文档(文本、布局描述、图像、视频、脚本等)完成了文档的完整重建。

为不同的 URL 设置路由:

app.js
const http = require('http');   
const port = 3000;
  
// Creating Basic http Server
const server = http.createServer((req, res) => {    
  const url = req.url;  // requested url
  const method = req.method;  // requested path
  if(url === "/")  // setting response for / path 
  {
      res.end("Path /");
  }
  // setting response for /about path
  else if(url === "/about")  
  {
   res.end("Path /about");
  }
  else  
  {
      // setting response for all other paths
      res.end("Path not found");
  }
  console.log("Url entered "+url);
});
server.listen(port, () => {
  console.log(`Server running at http://:${port}/`);
});


App.js
const http = require('http');
  
// File system module used for accessing files in nodejs
const fs = require("fs");  
const port = 3000;
  
// Helper function 
function readAndServe(path, res)   
{
    fs.readFile(path,function(err, data)
    {
        console.log(data);
  
        // res.setHeader('Content-Type', 'text/html');
        res.end(data);
    })
}
const server = http.createServer((req, res) => {  
  const url = req.url;
  const method = req.method;
  
  /* Serving static files on specific Routes */
  if(url === "/") 
  {
      readAndServe("./index.html",res) 
  
      /* The file named index.html will be sent 
         as a response when the index url is requested */
  }
  else if(url === "/about")
  {
      readAndServe("./about.html",res) 
      /*The about.html file will be sent as a response when 
        /about is requested */
  }
  else
  {
      res.end("Path not found"); 
      /* All paths other than / and /about will send an error as 
      a response */
  }
});
server.listen(port, () => {
  console.log(`Server running at http://:${port}/`);
});


index.html



  
    index
  
  
    

Welcome To GeeksForGeeks

    

This is Index file

    

        Click to go to About Page             

  


about.html


  
    About
  
  
    

Welcome To About Page

    

        Click to go to index             

  


输出:

使用 HTTP 模块提供静态文件:

应用程序.js

const http = require('http');
  
// File system module used for accessing files in nodejs
const fs = require("fs");  
const port = 3000;
  
// Helper function 
function readAndServe(path, res)   
{
    fs.readFile(path,function(err, data)
    {
        console.log(data);
  
        // res.setHeader('Content-Type', 'text/html');
        res.end(data);
    })
}
const server = http.createServer((req, res) => {  
  const url = req.url;
  const method = req.method;
  
  /* Serving static files on specific Routes */
  if(url === "/") 
  {
      readAndServe("./index.html",res) 
  
      /* The file named index.html will be sent 
         as a response when the index url is requested */
  }
  else if(url === "/about")
  {
      readAndServe("./about.html",res) 
      /*The about.html file will be sent as a response when 
        /about is requested */
  }
  else
  {
      res.end("Path not found"); 
      /* All paths other than / and /about will send an error as 
      a response */
  }
});
server.listen(port, () => {
  console.log(`Server running at http://:${port}/`);
});

索引.html




  
    index
  
  
    

Welcome To GeeksForGeeks

    

This is Index file

    

        Click to go to About Page             

  

about.html



  
    About
  
  
    

Welcome To About Page

    

        Click to go to index             

  

输出:将相应地提供为不同路径指定的静态文件。