简体   繁体   English

在url中传递参数-nodejs

[英]passing parameter in the url - nodejs

I have a problem in nodejs, see if any one could help me out. 我在nodejs中遇到问题,看看是否有人可以帮助我。

I have a page.html to which I need to pass a param with value like 我有一个page.html我需要将参数传递给

page.html?s=1

I am not able to send it, when I send it I am taken to a blank page. 我无法发送,发送时将被带到空白页。

server.js looks like the following: server.js如下所示:

var app = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url') ;

var http = app.createServer(function handler(request, response) {

var filePath = '.' + request.url;
if (filePath == './')
    filePath = './index.html';

var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
    case '.js':
        contentType = 'text/javascript';
        break;
    case '.css':
        contentType = 'text/css';
        break;
    case '.json':
        contentType = 'application/json';
        break;
    case '.png':
        contentType = 'image/png';
        break;
    case '.jpg':
        contentType = 'image/jpg';
        break;
    case '.wav':
        contentType = 'audio/wav';
        break;
    case '.xml':
        contentType = 'text/xml';
        break;
}

fs.readFile(filePath, function (error, content) {
    if (error) {
        if (error.code == 'ENOENT') {
            fs.readFile('./404.html', function (error, content) {
                response.writeHead(200, {'Content-Type': contentType});
                response.end(content, 'utf-8');
            });
        }
        else {
            response.writeHead(500);
            response.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
            response.end();
        }
    }
    else {
        response.writeHead(200, {'Content-Type': contentType});
        response.end(content, 'utf-8');
    }
});

}).listen(3100);
console.log('Server started at 3100');

Let me know if you need more info. 让我知道您是否需要更多信息。

您所做的称为查询字符串, url parameterpage.html/:something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM