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.
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.