How do you make URLs like “www.example.com/site/123/456” instead of “www.example.com/site?key=123&note=456”?

  • What do you want to achieve? I am wondering how do you make urls like www.example.com/site/123/456 and not like www.example.site/site?key=123&note=456

DON’T GO TO THE SITE AS IT’S JUST AN EXAMPLE

  • What is the issue? I have no idea how to do it, and it remained like this www.example.com/GetDataStores?UniId=1500&ApiKey=l0rem¡psum&prefix=test&limit=50

  • What solutions have you tried so far? I searched up here but never ended up finding any

This is the code for the main script

const crypto = require("crypto")
const axios = require("axios")
const express = require("express")

const app = express();

app.use(express.static("public"));

app.get("/GetDataStores",async(req,res) =>{
  
  let UniId = req.body("UniId")
  let ApiKey = req.body("ApiKey")
  let prefix = req.body("prefix")
  let limit = req.body("limit")
  
  const data = {
  params:{
    'prefix': prefix,
    'limit': limit
    
  },
  headers: {
    'x-api-key': ApiKey
  }

  }
  
 await axios.get('https://apis.roblox.com/datastores/v1/universes/'+UniId+'/standard-datastores',data)
  .then(function (response) {
   console.log(response.data)
    res.json([response.data])
  })
  .catch(function (error) {
    // handle error
    console.log(error.response.data);
  })
  .finally(function () {
    // always executed
  });
})




app.get("/GetEntry",async function(req,res){
  
  
   let UniId = req.body("UniId")
  let ApiKey = req.body("ApiKey")
  let Name = req.body("name")
  
  const data = {
  params:{
    'dataStoreName': 'scripts/'+Name,
    'entryKey': '1'
    
  },
  headers: {
    'x-api-key': ApiKey
  }
  }
  
  await axios.get('https://apis.roblox.com/datastores/v1/universes/'+UniId+'/standard-datastores/datastore/entries/entry',data)
  .then(function (response) {
   console.log(response.data)
    res.json([response.data])
  })
  .catch(function (error) {
    // handle error
    console.log(error.response.data);
  })
  .finally(function () {
    // always executed
  });
})

app.get("/GetEntries",async function(req,res){
  

  
  const data = {
  params:{
    'dataStoreName': 'scripts/Jeff',
    'prefix': '',
    'limit': '2'
    
  },
  headers: {
    'x-api-key': process.env.Api_KeyImportant1
  }
  }
  
  await axios.get('https://apis.roblox.com/datastores/v1/universes/4442833374/standard-datastores/datastore/entries',data)
  .then(function (response) {
   console.log(response.data)
    res.json([response.data])
  })
  .catch(function (error) {
    // handle error
    console.log(error.response.data);
  })
  .finally(function () {
    // always executed
  });
})

app.get("/SetEntry",async function(req,res){
  let msg = req.param("msg")
  let UniId = req.param("UniId")
  let ApiKey = req.param("ApiKey")
  let Name = req.param("name")
  
  
  console.log(msg)
  let stuff = JSON.stringify(msg)
  console.log(stuff)
  let idk = crypto.createHash("md5").update(stuff).digest("base64")
  console.log(idk)
  const data = {
  params:{
    'datastoreName': "scripts/"+Name,
    'entryKey': '1'
    
  },
  headers: {
    'x-api-key': ApiKey,
    'content-md5': idk,
    'content-type': 'application/json'
  }

  }
  
 await axios.post('https://apis.roblox.com/datastores/v1/universes/'+UniId+'/standard-datastores/datastore/entries/entry',msg,data)
  .then(function (response) {
   console.log(response.data)
    res.json([response.data])
  })
  .catch(function (error) {
    // handle error
    console.log(error.response.data);
  })
  .finally(function () {
    // always executed
  });
})



const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});

Here’s the package.json

{
  "name": "nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@types/node": "^18.0.6",
    "axios": "^1.4.0",
    "express": "^4.18.2",
    "noblox.js": "^4.14.1",
    "node-fetch": "^3.2.6"
  }
}
1 Like
2 Likes

As @UMARismyname already pointed it out. You can use URL parameters using req.params from the Express App.

For example:

app.get("/GetDataStores/:UniId/:ApiKey/:prefix/:limit", async (req, res) => {
  let UniId = req.params.UniId;
  let ApiKey = req.params.ApiKey;
  let prefix = req.params.prefix;
  let limit = req.params.limit;

  // ...
});

app.get("/GetEntry/:UniId/:ApiKey/:name", async function (req, res) {
  let UniId = req.params.UniId;
  let ApiKey = req.params.ApiKey;
  let Name = req.params.name;

  // ...
});

app.get("/SetEntry/:UniId/:ApiKey/:name/:msg", async function (req, res) {
  let UniId = req.params.UniId;
  let ApiKey = req.params.ApiKey;
  let Name = req.params.name;
  let msg = req.params.msg;

  // ...
});

That way you can generate more clean URL’s. Taking SetEntry for example:

/SetEntry/900/details/ScriptName/HelloJohnDoe

Read their documentation to know more about.

Ps.: Don’t forget to update your route definition!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.