URL rewriting Node js - dynamic URLs - javascript

This seems to be a Noob question, but still i dont know how to go about with this.
Im using node js for my server side development and recently came across SEO friendly URLs.
How do i rewrite the URLS which are something like www.abc.com/meet_team.html to
www.abc.com/meet-the-team/
One answer would be to use controllers and to route to html pages. This works fine for static web pages.
My problem is with dynamic data. To quote an example, Lets say, yts.ag page has movies stored in the db and it gets retrieved and the url changes dynamically.
For example : www.yts.ag/movies/the-revenant - > This would pick up details about the movie revenant. If the change it to movies/the-dark-knight, it would do so accordingly. Here movies would be the controller, In my Node code, i would handle it something like this.
app.get('/:controller/:movieName', func(req, res){
// get controller and movie name from req object and query from DB and respond back.
})
Now the problem is, i would have stored the name of movie as "The Revenant" in the DB. I would be getting the movie name from the GET request as "the-revenant". How do i query this from the database?
Should i parse the param first? strip the hyphen and then pass it to the DB or any other solution?
Please help me on this. I dont know whether this is the right approach.
Thanks in advance.

It is as you describe. Now you have to retrieve the movie from the slug reference.
Look for mongo or mysql

Related

Netsuite SuiteTalk API GET Index Not Returning Full Resource

I'm totally confused.. I finally got the SuiteTalk API working and am able to make authenticated calls from my JS server..
I'm calling the GET /invoice route, which documented here:
https://system.netsuite.com/help/helpcenter/en_US/APIs/REST_API_Browser/record/v1/2022.2/index.html#tag-invoice
Says that it should return to me an InvoiceCollection which is a collection of Invoices which, by the documentation, has all the fields I need as part of an Invoice.
However, the actual response I am getting back is just an array of items that only has the ID in it.. where are all the other invoice fields?!
I tried to just fire off subsequent calls to the /invoice/{id} route with each ID, but I am getting back some of them as unauthorized and some not.. assuming having to do with timestamp being to close together or something.
There has to be a way to call up to /invoice and get all of the data having to do with an invoice and not just the ID right?
I'm also wondering how I can utilize the "Q" param to filter to invoices only for a certain customer, or date, etc. Appreciate any help!
I recommend trying all these queries in postman (netsuite provides a collection for it). If you try it there, you'll see that {{REST_SERVICES}}/record/v1/invoice returns a list of id's and links to invoices.
If you dont see the details of an invoice, can that have something to do with the rights (to that subsidiary)?
e.g. GET: {{REST_SERVICES}}/record/v1/invoice/164198 shows me the entire record.
To get invoices from a certain customer i think i would go for a query in the suiteql restlet.
Hope that helps a bit. I'm just discovering suitetalk myself and it's not the easiest to get a grip of.

NextJS How to create dynamic backend routes

Is there away for me to create dynamic backend routes? I am creating and image host. I am wanting the user to be able to get their image saved on the server under a domain like this http://localhost/<random_id> and example of the link would be http://localhost/a3Fafght5, I have looked around online and I could not find anything about creating dynamic backend routes and then when I did find one thing it said I needed to use getStaticPaths to declare all the possible ids. I dont know what the id is going to be when I build the project, I need to be able to query the database with it and check if it exists and do things from there.
You can use dynamic page routing if you have file like pages/[imageId].js
and then simply put getServerSideProps in your file which can call your database and determine if this is valid ID or not. For valid ID your would return image, for not valid simply 404.
If you don't want to have server-side rendering, but static one instead. You could have the same file as above and have getStaticPaths function which would query the database and return array of all possible IDs. This however could be issue if you have a lot of images, then the server-side solution would be easiest.

Create Temporary Webpage with Randomly Generated URL

Alright, so I'm trying to write a little app that creates a temporary webpage (kinda) and I can't figure out what it is that needs to happen for this to work exactly.
Let me explain what I'm trying to do and that should help. Basically, I want to have app.com and when a user goes there there's a button like "Make temp page" or something. Click that and then it makes a random URL extension and puts the visitor there, so something like app.com/admin/xxyy12. Now the user can give the URL app.com/xxyy12 to someone and they can navigate there and send data back and forth between each other, my thinking was via WebSockets. So I want the person that made the link to be an admin of sorts and when they leave that "admin" page I want it and app.com/xxyy12 to self destruct basically, no one can go there and that route doesn't exist anymore.
Is this possible and what technologies should I look into to accomplish this? I thought it would be cool to do this all in-browser with no server, which kills some security but I don't really care about that. I'm not really sure how to create these "temporary" pages either. I figured WebSockets would come in handy to send data between the person that created the link and the client visiting it, and I figured the main page app.com would need a socket to the user at app.com/admin/xxyy12 to determine when it disconnects. Also, I considered html parameters to basically make a "temporary" URL that would just read the param and then that would wire the client to the URL creator. There would also need to be a way of storing what active temporary pages exist.
I recognize that this question is rather vague but I suppose I'm just looking for ideas of how to accomplish this and what technologies would be recommended. Thanks for any and all help.
This should be easy to implement.
I would use:
Node.js on the server with
express.js and maybe socket.io if needed
In express:
create routes to serve pages
app.get('/', function(req, res){
//render homepage here
});
app.post('/', function(req,res){
// handle creating temp pages here and
// redirect client to that page
// save ID of the page to db
});
app.get('/:pageId', function(req,res){
// handle rendering the temp page by ID
});
app.post('/destroy', function(req,res){
// remove page from db
});
You could post from client on javascript unload message i think, so maybe no need for websockets but im not sure how reliable that is
I hope this helps
I figured out how to get what I wanted. I basically make a random string and add it to the end of the url with a '#' at the beginning. Then I can use location.hash.substring to pull off the random string. This string acts as "unique" identifier to connect peers together.

How to dynamically create a JSON file for each Object that gets added to another JSON file

I would like to dynamically create a corresponding JSON file every time a new merchant signs up to my site.
For example:
Burger King signs up to be a merchant on my site. I add Burger king to my merchants.json file. How would I dynamically create a file that gets inserted into that JSON object that can later be used to pull up data specific to that merchant, on that merchants page. For example, a JSON file full of products or deals.
Is this even the right way to go about it?
Can someone point me in the right direction please?
This seems like a very common usage scenario but I don't see any examples online that explain this application structure thoroughly.
NOTE: I am using AngularJS
EDIT: Thanks for the tips guys, after asking around in the #AngularJS channel on IRC, I've been told to go the extra mile and create an API. I'll probably use sails.js to help with that. This obviously isn't how I was planning to do things, but as you guys pointed out, it wasn't the best practice; not by a long shot.
1) You'd need a small server-side PHP script that accepts the new JSON file sent by the client
2) Browser requests merchants.json from the server
3) Load it with JSON.parse()
4) Add the merchant to the Object
4) JSON.stringify(object)
5) Send back to the server.
Now, this is a horrible horrible idea. Use a server-side database for storing any kind of information on your clients -- MySQL, whatever. JSON is for transporting data, not storing it. You can write some PHP scripts to dynamically generate a page for your merchant based on the data in the database -- so much easier and so much more secure. The method above will expose the whole client database to the client, and based on your specifications above, I don't see another way.

Efficient way to pass arrays in url

I am building a webapp and have a few arrays that I would like to pass through the URL in order to make the results of my application easily sharable.
Is there an efficient way to do this? I know a lot of websites (like youtube) use some sort of encoding to make their URLs shorter, would that be an option here?
Thanks in advance!
What I suspect you're asking is you have some page where the user can alter information, etc, and you want a way to create a URL on the fly with that information so it can easily be accessed again. I've listed two approaches here:
Use the query string. On your page you can have a button saying "save" that produces a URL with info about what the user did. For example, if I have a webpage where all I do is put my name in and select a color, I can encode that as http://my-website.com/page?name=John_Doe&color=red. Then, if I visit that link, your page could access the query object in JavaScript and load a page with the name and color field already set.
An approach for the "YouTube-style" URLs would be to create a hash of the relevant information corresponding to the page. For example, if I were creating a service for users to store plaintext files. These files are to have the following attributes: title, date, name, and body. We can create a hash of the string hash_string = someHashFunction(title+date+name).
Of course, this is a very naive hashing scheme, but something like this may be what you are looking for. Following this, your URL would be something like http://my-website.com/hash_string. The key here is not only creating these URLs, but having a means to route requests on the server side to the page corresponding to the hash_string.

Categories

Resources