- clone the repo
- run the installation of node modules in your console it should install the dependencies in your root folder
npm i- starting the servers
node restServer configComputers.json
node restServer configBook
node restServer configPersonyou can run all 3 servers simultanuously
- open the restTester.html in your browser
Then you have the option to check the route when you select "get" and press submit, you can see which server runs on the address localhost:xxxx/api/
when you write it to the route, and hit get again, it will show you all data of that route
computers runs on port:4000
persons runs on port:4001
books runs on port:4002
- now you can test several methods for each server
hit get and copy one object to the JSON body field and try it with submit
put will update an object post will post a new object delete will delete an object
copy all the storageLayer, server, and storage files from previous project, then modify
here update config json with storages and folders
{
"port":4000,
"host":"localhost",
"engineFolder":"storageEngines",
"storageFolder":"storages",
"storageEngine":{
"folder":"storageLayer",
"dataStorageFile":"dataStorageLayer.js"
},
"storage":{
"folder":"computerStorage",
"storageConfigFile":"storageConfig.json"
}
}Filename: storageConfig.json needs to match the one in the config.json
{
"storageFile":"computers.json",
"adapterFile":"computerAdapter.js",
"primaryKey":"id",
"resource":"computers"
}we need to create a class with a constructor, that takes the storageConfig.json values as parameters to use in the functions
I am taking that to the storageConfig
requiring that file to this adapt -- from the adapter function
// copied functions updated with this.# // replace storageFilePath with this.#storageFilePath
then create a new file: configPerson
{
"port":4001,
"host":"localhost",
"engineFolder":"storageEngines",
"storageFolder":"storages",
"storageEngine":{
"folder":"storageLayer",
"dataStorageFile":"dataStorageLayer.js"
},
"storage":{
"folder":"personStorage",
"storageConfigFile":"storageConfig.json"
}
}console.log(process);
console.log(process.argv);contains operating system and lots of useful things
node commandLineArguments.js 1 2 3 4 5node commandLineArguments.js 1 2 3 4 5
[
'/usr/local/bin/node',
'/Users/s2400789/Documents/BC_Studies/12_Node-Advanced/week4/param-Version/commandLineArguments.js',
'1',
'2',
'3',
'4',
'5'node commandLineArguments.js 12345
[
'/usr/local/bin/node',
'/Users/s2400789/Documents/BC_Studies/12_Node-Advanced/week4/param-Version/commandLineArguments.js',
'12345'
] node commandLineArguments.js 1 2 3 jhkjhk "this is a
string"
[
'/usr/local/bin/node',
'/Users/s2400789/Documents/BC_Studies/12_Node-Advanced/week4/param-Version/commandLineArguments.js',
'1',
'2',
'3',
'jhkjhk',
'this is a string'
]console.log("length", process.argv.length); node commandLineArguments.js 1 2 3 jhkjhk "this is a string"
length 7we get everything as a string and we can get rid of the first 2 arguments that contain: argv[0] => program name (node) argv[1] => filepath
const [, , ...values] = process.argv;
console.log(values);node commandLineArguments.js 1 2 3 jhkjhk "this is a string"
[ '1', '2', '3', 'jhkjhk', 'this is a string' ]
console.log('+++++++++++++');
for (let i = 0; i < values.length; i++) {
console.log(`argv[${i}] = ${process.argv[i]}`);
}
console.log('values');
for (let i = 0; i < values.length; i++) {
console.log(`values[${i}] = ${values[i]}`);
}"use strict";
const path = require("path");
const [, , configFileName] = process.argv;
if (process.argv.length < 3) {
console.log("Please provide a parameter");
} else {
const config = require(path.join(__dirname, configFileName));
console.log(config); // if you dont giv the name it will not run
}we pass the config to our server and we can use it in the server
now we can see the files:
node testServer.js configPerson.json;we need to change the restServer to run it from command line
but then you can provide your argument for starting the server via console. Cool!
put the testServer.js code to the restServer.js
Not functioning: get keys computers keys : empty array