簡體   English   中英

僅在 NodeJS 中更改 URL

[英]Changing URL in NodeJS only

我的服務器回調中有以下代碼段:

switch (request.url) {
    case "/somescript" :
        response.writeHead(200, {"Content-Type": "text/javascript"});
        response.write(somescript);
        break;      
    default :
        response.writeHead(200, {"Content-Type": "text/plain; charset=UTF-8"});
        response.write(html);
}
response.end();

當我運行服務器時,當我輸入:

localhost:3000

在瀏覽器上,如何讓它自動更改為:

localhost:3000/changed

只使用 Node.js(在我的 switch 語句的默認部分)?

只需添加response.writeHead(301, {"Location": "/changed"}); 默認。

switch (request.url) {
    case "/somescript" :
        response.writeHead(200, {"Content-Type": "text/javascript"});
        response.write(somescript);
        break;      
    case "/changed" :
        response.writeHead(200, {"Content-Type": "text/plain; charset=UTF-8"});
        response.write(changedhtml);
        break;
    default :
        response.writeHead(301, {"Location": "/changed"});`
}
response.end();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM