【问题标题】:How to show different pages depending on URL?如何根据 URL 显示不同的页面?
【发布时间】:2018-12-27 06:35:34
【问题描述】:

我正在学习使用 Javascript 和 nodejs 制作自己的主页。

我练习了只有一页的各种示例。

但是,我想知道如何连接主页内的每个页面?

例如,

https://stackoverflow.com/

这是stackoverflow主页的url。

https://stackoverflow.com/questions/ask

当我点击询问时,页面会更改为不同的网址。

我没有在 google 或 youtube 上搜索过此类示例。

您能推荐一些关于 google 或 youtube 的教程来学习这些吗?

这样的例子怎么说?

非常感谢。

【问题讨论】:

标签: javascript node.js


【解决方案1】:

这称为“路由”,您可以在客户端或服务器端进行。

基本上你在你的服务器中创建一个大的switch 语句,或几个if's

const url = require('url');
const http = require('http');

http.createServer(function(req,res){
   const path = url.parse(req.url).pathname;
   // If or switch
   if (path === '/'){
     res.send(stuff);
   } else {
     res.send(otherStuff);
   }
}).listen(3000);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 2016-11-19
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多