Javascript 如何在url use Node.js中添加我的web应用程序名称?
如何在url use Node.js中添加我的web应用程序名称 我的网络运行与 我想添加路径名/myapp,如Javascript 如何在url use Node.js中添加我的web应用程序名称?,javascript,node.js,express,Javascript,Node.js,Express,如何在url use Node.js中添加我的web应用程序名称 我的网络运行与 我想添加路径名/myapp,如 如果您使用的是express,请遵循本文档 在vanilla Node js中,您可以这样做 const http = require('http'); const url = require('url'); // Routes const router = { /* '/myapp' route will be handled here and following fu
如果您使用的是express,请遵循本文档 在vanilla 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 ');
});
consthttp=require('http');
const url=require('url');
//路线
常数路由器={
/*“/myapp”路由将在此处处理,并将执行以下功能*/
“myapp”:函数(回调){
//我们期望回调函数作为参数
回调(200,{name:'sample handler'});
//执行回调函数并传递200个状态代码和一个样本数据,这些数据将作为响应负载发送给用户
},
“登录”:函数(回调){
回调(200,{name:'Login'});//您可以在这个对象中添加路由并处理它们
}
};
const server=http.createServer((req,res)=>{
//获取URL
const parsedURL=url.parse(req.url,true);
//获取路径
const path=parsedURL.pathname;//这将给出**http://127.0.0.1:8080/myapp **
const trimmedPath=path.replace(/^\/+\/+$/g',);
//在我们的示例**myapp中,这将获得在基本url之后指定的路由**
请求on('数据',(数据)=>{
//流数据。浏览器将在流中发送数据,此函数将在每个流上运行
});
//当请求结束时,可以在此事件中处理所有用户请求
请求开启('end',()=>{
//处理路线
const choosenHandler=路由器[trimmedPath];//签入路由obj
//发送所需的回调函数
选择处理程序(功能(状态代码、有效负载){
//向用户发送响应
const payloadString=JSON.stringify(有效载荷);
res.setHeader('Content-Type','text/html');
res.writeHead(状态代码);
res.write('Data:'+payloadString+'');//这将发送给用户
log(“返回响应”);
res.end();
});
});
});
//启动服务器
服务器。听(3000,()=>{
log('服务器监听3000端口');
});
听起来您需要了解路由。您可以定义如下所示的路径参数。了解路由。还可以配置您的服务器以将任何请求从特定路径名重定向到您的应用程序。是的,在server.js i设置应用程序中。使用('appName',routes),但我遇到错误,说找不到我的登录页可能会使用上述代码加上errorI设置路由更新您的问题。获取('/',errorHandle(viewLogin))以重定向我的登录页,并设置routes.post('/login',errorHandle(login));对于我的登录管理员Hanks,我发现了我的错误,因为我没有更新我的路由,谢谢您的回答