如何使用Node.js在URL中添加我的Web应用程序名称?

红宝石

如何使用Node.js在URL中添加我的Web应用程序名称?

我的网站使用http://127.0.0.1:8080运行

我想添加路径名/ myapp

http://127.0.0.1:8080/myapp

普拉纳夫·信德

如果您使用快递,请遵循此文档

https://expressjs.com/en/guide/routing.html

在香草Node js中,您可以执行此操作

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

// Routes
const router = {
    /* '/myapp' route will be handled here and following function will be executed */
    'myapp': function (callback) {
        //we expect a callback function as argument
        callback(200, { name: 'sample handler' });
        //execute the callback function and pass 200 status code and a sample data which will send as response payload to user
    },
    'login': function(callback){
        callback(200, { name: 'Login' }); //You can add your routes in this object and handle them
    }
};

const server = http.createServer((req, res) => {
    // Get URL
    const parsedURL = url.parse(req.url, true);

    // Get Path
    const path = parsedURL.pathname; //This will give **http://127.0.0.1:8080/myapp **
    const trimmedPath = path.replace(/^\/+|\/+$/g, '');
    //This will get routes specified after base url in our case **myapp**

    req.on('data', (data) => {
        // streamed data. Browser will send data in stream, this func will run on each stream
    });

    // When Request in ended, all user requests can be handled in this event
    req.on('end', () => {
        // Handle Routes
        const choosenHandler = router[trimmedPath]; //Check in route obj

        //Send the callback function which was required
        choosenHandler(function (statusCode, payload) {
            // Send response to user
            const payloadString = JSON.stringify(payload);

            res.setHeader('Content-Type', 'text/html');
            res.writeHead(statusCode);
            res.write('<h1>Data: ' + payloadString + '</h1>'); //This will be sent to user
            console.log('returning response');
            res.end();
        });
    });
});

// Start the server
server.listen(3000, () => {
    console.log('Server Listening to 3000 port ');
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在“默认程序”列表中添加我的应用程序

如何为Web应用程序设置tomcat上下文路径?.war文件名位于url中应用程序名称的前面

Django URL映射:如何从URL路径中删除应用程序名称?

没有会话ID,如何找到应用程序的内容URL。我只知道应用程序名称

如何在jConsole本地进程中添加我的应用程序?

如何在本机应用程序名称中添加空间?

如何在Postgresql JDBC URL中设置应用程序名称?

如何删除URL中的应用程序名称?Tomcat + httpd

如何在.net mvc中获取带有应用程序名称的URL?

如何从Weblogic上的URL中删除Java应用程序名称

如何在django中重写基本url以在所有页面的url中添加登录的用户名而不是应用程序名称?

如何在VisualVM中更改应用程序名称?

如何在laravel中获取应用程序名称?

如何在NativeScript中更改应用程序名称

如何配置Visual Studio在URL中包含我的Web应用程序的名称?

IBM Worklight-如何在访问适配器的URL路径中更改应用程序名称

如何从进程名称获取应用程序名称?

如何在 Dock 中显示文档名称而不是应用程序名称?

如何在golang lib / pq postgresql驱动程序中设置应用程序名称?

如何在Apple Store中更改应用程序的提供程序名称?

[Angular,Jersey]为什么我必须使用Jersey在$ http中指定应用程序名称

如何在 Django 中使用 app_name 包含 url 而不必使用反向应用程序名称?

如何增加我的 android 设备中的应用程序图标大小,用于基于颤振的应用程序

如何在JNLP应用程序中修复“缺少代码库,权限和应用程序名称清单属性”?

如何在UITesting Swift 4 iOS中获取应用程序名称

如何在Laravel 6中更改应用程序名称(空间)

在Hammerspoon中,如何在启动时获得当前关注的应用程序名称?

如何在Xamarin.Forms中更改Android应用程序名称?

如何在Grails 3中更改应用程序名称?