2

I'm planning on converting a small PHP codebase (less than 1000 lines) to Node.js. It's a web app that contains about 50k lines of JS, so I figured I'd move it to Node.js. (Plus, PHP sucks donkey balls.) There's a few static files (CSS, HTML, etc.) that I'll be serving with the node-static package. My issue is with my PHP files that are not static.

For example, I have a search page that runs on a GET variable. I tack on ?keyword=blah onto the end of the URL and the PHP script searches the DB and outputs HTML for the browser. How do I replicate this behavior in Node.js? (In other words, how can I create a server to listen at localhost/search.html and accept GET variables in the URL?)

I know I can change the way searches are done to accomplish this, but I'd really like to figure out how to replicate this behavior since I have several scripts that act the same way.

EDIT: As for the database, I'm actually using OrientDB. But I don't connect directly to it, I use a set of Java services that I post JSON requests to. So really, all I need to be able to do is post a JSON request (synchronously) and output HTML.

7
  • What kind of database are you using? Are you able to move to Mongo? Commented Mar 27, 2013 at 14:55
  • Sorry, I'll clarify that in the original question. Commented Mar 27, 2013 at 14:56
  • 2
    Take a look at Express, that's a pretty easy framework which can handle both static and dynamic requests. Commented Mar 27, 2013 at 14:58
  • Wow, I can't believe I searched for over an hour and Express didn't come up. Thank you, it's exactly what I need. Feel free to answer the question with that and I'll accept. Commented Mar 27, 2013 at 15:07
  • @GJK I gave you code sample :) Commented Mar 27, 2013 at 15:10

1 Answer 1

3

Here's how I do it:

Create a new ExpressJS app

"express myapp" 

Install all the dependencies

"cd myapp && npm install".

In app.js, make sure you require the "url" package before "var app"

var url = require('url');

Add a new route to "app.js" so that it looks for any GET requests to "search.html"

app.get('/search.html', function(req, res) {

    // Process the request for the GET variables
    var url_parts = url.parse(req.url, true);
    var query = url_parts.query; // These are all your GET variables
    var search_keyword = query.keyword;

    if(search_keyword) {
        // The keyword indeed exists
        console.log(search_keyword);

    }
    res.end();
});

If you run the app and go to "search.html?keyword=haha", your console will output "haha". You can do anything after that with the keyword.

Sign up to request clarification or add additional context in comments.

1 Comment

If any of you people in the future are using Typescript, node.d.ts from MS describes the query member as a string when it's really an object. Just FYI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.