728x90

Overview

When you are working on creating REST API endpoints using Node.js, Express, and TypeScript, you might come across the need to utilize request parameters and query parameters. While both of these parameters are used for similar purposes, they are not the same. I’ll discuss the difference between Request Parameters and Query Parameters, I will also add code examples and the ideal time to use query parameters and request parameters.

They are both used to pass data to the server. However, they are used in different ways. request.params is used to pass parameters through the URL path, while request.query is used to pass parameters through the URL query string.

Using Request Parameters

Request parameters are used to send data to the server through the URL path. An example of a URL with a request parameter would look like this: http://127.0.0.1:5001/api/users/123. In this example, "123" is the request parameter. You can access request parameters using the request.params object. Here's an example code snippet:

app.get('/api/users/:id', (req, res) => {
  const userId = req.params.id;
  // Do something with userId
});

Here, we’re using a GET request to retrieve a user’s information by their ID. We’re passing the ID as a request parameter through the URL path, and we’re accessing it using request.params.id.

Using Query Parameters

Query parameters are used to send data to the server through the URL query string. An example of a URL with a query parameter would look like this: http://127.0.0.1:5001/api/users?id=123. In this example, "id" is the query parameter key, and "123" is the query parameter value. You can access query parameters using the request.query object. Here's an example code snippet:

app.get('/api/users', (req, res) => {
  const userId = req.query.id;
  // Do something with userId
});

In this example, we’re using a GET request to retrieve a user’s information by their ID. We’re passing the ID as a query parameter through the URL query string, and we’re accessing it using request.query.id.

Query Parameters vs. Request Parameters

Query parameters are ideal for filtering data or passing optional parameters to an API endpoint. For example, if you’re building an API endpoint that returns a list of users, you might want to filter the results by age, gender, or location. You can achieve this by using query parameters.

Request parameters are ideal for passing mandatory parameters to an API endpoint. For example, if you’re building an API endpoint that returns a user’s information by their ID, you’ll want to pass the ID as a request parameter.

728x90

 would like to know how I can represent complex typedef like the following in UML:

class Foo {
protected:
   typedef std::pair<uint64_t, Bar> BarPair;
   typedef std::set<BarPair> BarPairSet;

   std::map<uint32_t, BarPairSet> m_indexedBarPairSet
};

Bar is not part of Foo.

 

There is no direct way to represent your typedefs, of course it is possible to have for instance the class BarPair with a dependency to the template class pair but there is no standard way to support the binding.

In case it is not for instance typedef std::pair BarPair but BarPair : public std::pair {} we have

So what about to extend UML with the stereotype typedef to have something like that :

or to stereotype the realization with typedef-bind and to use the stereotype typedef for BarPair and BarPairSet

 

728x90

What is the purpose of com.google.common.base.Verify when we have com.google.common.base.Preconditions?

The Verify class looks nice but it has an @Beta annotation, should I use it?

 

 

The difference is semantic. Verify is used to ensure that invariants don't change, that Code which has been engineered to do a certain thing is actually doing that thing. In spirit:

int x = divide(10, 5);
Verify.verify(x == 2, "X should be 2");

 

Preconditions, on the other hand, are expected to fail when bad input is passed to a certain portion of the program, usually either from the user to the code, or from client code to code within another library. In spirit:

public int divide(int x, int y) {
  Preconditions.checkArgument(y != 0, "Can't divide by 0!");
  return x / y;
}

As to whether you should use a @Beta class, that entirely depends on the scope and foreseeable lifetime of the application you are building, and, asked alone, would probably be flagged as a "Primarily Opinion-Based" question.

'Java' 카테고리의 다른 글

[Spring] ST_intersects method  (1) 2024.09.09
[Pageable] withSort  (0) 2024.08.30
Spring Batch - Chunk 지향 프로세싱(Mybatis)  (0) 2024.07.17
Pattern and Matcher  (0) 2024.07.16
SecureRandom  (0) 2024.07.12
728x90

HashiCorp develops an official VMware Fusion and VMware Workstation provider for Vagrant. This provider allows Vagrant to power VMware based machines and take advantage of the improved stability and performance that VMware software offers.

This provider allows Vagrant to power VMware based machines and take advantage of the improved stability and performance that VMware software offers. Source of an image: vagrantup.com
 

Vagrant is an open-source software product developed by Hasicorp for building and maintaining portable virtual software development environments; e.g., for VirtualBox, KVM, Hyper-V, Docker containers, VMware, and AWS. Vagrant can quickly spin up your virtual environment and tear it down after use.

 

Overview

  • Download and Install Vagrant
  • Install required plugins for Vagrant
  • Identify the right Vagrant boxes for & fusion environment
  • Deploy the virtual environment and connect to the server
 

We will show a small example of using Vagrant with VMware Fusion in a local environment. I use VMware Fusion Pro 13.5.

You can download VMware Fusion from: https://www.vmware.com/products/fusion/fusion-evaluation.html

https://customerconnect.vmware.com/downloads/info/slug/desktop_end_user_computing/vmware_fusion/13_0

Download and Install Vagrant

We go to the website: https://developer.hashicorp.com/vagrant/install

and download the appropriate version for the OS. In my case, I’m downloading AMD64 (Version: 2.4.0) and we will start a simple installation.

 

Okay, its time to validate the vagrant installation. Open the mac terminal and type “vagrant” command.

On the successful execution you can able to see all these command options and also there is a help option available to get more information abount the vagrant command line.

vagrant
Usage: vagrant [options] <command> [<args>]

    -h, --help                       Print this help.

Common commands:
     autocomplete    manages autocomplete installation on host
     box             manages boxes: installation, removal, etc.
     cloud           manages everything related to Vagrant Cloud
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     serve           start Vagrant server
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     upload          upload to machine via communicator
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version
     winrm           executes commands on a machine via WinRM
     winrm-config    outputs WinRM configuration to connect to the machine

Install VMware Plugins for Vagrant

Make vagrant to communicate with VMware Fusion so we have to install VMware plugins so that vagrant can talk to VMware Fusion to create the virtual machines.

vagrant plugin install vagrant-vmware-desktop
Installing the 'vagrant-vmware-desktop' plugin. This can take a few minutes...
Fetching vagrant-vmware-desktop-3.0.3.gem
Installed the plugin 'vagrant-vmware-desktop (3.0.3)'!

See the available plugin using vagrant plugin list.

vagrant plugin list
vagrant-vmware-desktop (3.0.3, global)

To check the plugin agent you can use launch ctl command to list the vagrant vmware utility.

launchctl list | grep com.vagrant.vagrant-vmware-utility

Ref: Vagrant VMware Utility Installation and Install Vagrant VMware Utility

 

Install | Vagrant | HashiCorp Developer

Explore Vagrant product documentation, tutorials, and examples.

developer.hashicorp.com

 


이 경우에 git bash 에서 launchctl을 못찾게 되는 불상사가 생기는데..

그래서 다음 스텝으로 강제로 올려버리면

 

ubuntu를 사용할 경우

vagrant init bento/ubuntu-16.04
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.


vagrant up
Bringing machine 'default' up with 'vmware_desktop' provider...
==> default: Box 'bento/ubuntu-16.04' could not be found. Attempting to find and install...
    default: Box Provider: vmware_desktop, vmware_fusion, vmware_workstation
    default: Box Version: >= 0
==> default: Loading metadata for box 'bento/ubuntu-16.04'
    default: URL: https://vagrantcloud.com/api/v2/vagrant/bento/ubuntu-16.04
==> default: Adding box 'bento/ubuntu-16.04' (v202112.19.0) for provider: vmware_desktop
    default: Downloading: https://vagrantcloud.com/bento/boxes/ubuntu-16.04/versions/202112.19.0/providers/vmware_desktop/unknown/vagrant.box
    default:
==> default: Successfully added box 'bento/ubuntu-16.04' (v202112.19.0) for 'vmware_desktop'!
==> default: Cloning VMware VM: 'bento/ubuntu-16.04'. This can take some time...
==> default: Checking if box 'bento/ubuntu-16.04' version '202112.19.0' is up to date...
==> default: Verifying vmnet devices are healthy...
==> default: Preparing network adapters...
==> default: Starting the VMware VM...
==> default: Waiting for the VM to receive an address...

이래도 4만년 걸리는 것은 마찬가지 

난 그래서 우분투 이미지 다운로드 받아서 쓰기로 했다. 

 

 

Identify the required Vagrant boxes

Lets identify the vagrant boxes you need to select carefully by choosing the right architecture to make the background up and running.
Go back to the vagrant portal again and look:

https://app.vagrantup.com/hashicorp/boxes/bionic64

as an example: hashicorp/bionic64

click on “new” and copy the command and paste it in the terminal window where you run the commands, i.e. locally.

vagrant init hashicorp/bionic64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
lubomir@L-MAC ~ % vagrant box add hashicorp/bionic64
==> box: Loading metadata for box 'hashicorp/bionic64'
    box: URL: https://vagrantcloud.com/api/v2/vagrant/hashicorp/bionic64
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) virtualbox
3) vmware_desktop

Enter your choice: 3
==> box: Adding box 'hashicorp/bionic64' (v1.0.282) for provider: vmware_desktop
    box: Downloading: https://vagrantcloud.com/hashicorp/boxes/bionic64/versions/1.0.282/providers/vmware_desktop/unknown/vagrant.box
==> box: Successfully added box 'hashicorp/bionic64' (v1.0.282) for 'vmware_desktop'!

So this command will create the vagrant file on the current directory and all you need to do is that just vagrant up in order to bring up this machine. The back end vagrant trying to communicate with the vmware fusion.

Deploy the virtual environment and connect to the server

vagrant up                                                   
Bringing machine 'default' up with 'vmware_desktop' provider...
==> default: Cloning VMware VM: 'hashicorp/bionic64'. This can take some time...
==> default: Checking if box 'hashicorp/bionic64' version '1.0.282' is up to date...
==> default: Verifying vmnet devices are healthy...
==> default: Preparing network adapters...
==> default: Starting the VMware VM...
==> default: Waiting for the VM to receive an address...
==> default: Forwarding ports...
    default: -- 22 => 2222
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Configuring network adapters within the VM...
==> default: Waiting for HGFS to become available...
==> default: Enabling and configuring shared folders...
    default: -- /Users/lubomir: /vagrant

Vagrant will create a virtual machine that will start when the mission starts, you should be able to login to the machine using ssh.

vagrant ssh
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-58-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Tue Jan  2 20:59:01 UTC 2024

  System load:  0.0               Processes:           95
  Usage of /:   2.6% of 61.80GB   Users logged in:     0
  Memory usage: 15%               IP address for eth0: 192.168.212.172
  Swap usage:   0%

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

0 packages can be updated.
0 updates are security updates.

Check the os version which you have deployed.

uname -a
Linux vagrant 4.15.0-58-generic #64-Ubuntu SMP Tue Aug 6 11:12:41 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

VM port forwarding to host

Open the vagrant file using a studio code so that we can make changes to the vagrant file easily. Here I have added a line in order to forward the port from guest to host.

vagrant reload 
==> default: Discarding suspended state...
==> default: Checking if box 'hashicorp/bionic64' version '1.0.282' is up to date...
==> default: Verifying vmnet devices are healthy...
==> default: Preparing network adapters...
WARNING: The VMX file for this box contains a setting that is automatically overwritten by Vagrant
WARNING: when started. Vagrant will stop overwriting this setting in an upcoming release which may
WARNING: prevent proper networking setup. Below is the detected VMX setting:
WARNING: 
WARNING:   ethernet0.pcislotnumber = "160"
WARNING: 
WARNING: If networking fails to properly configure, it may require this VMX setting. It can be manually
WARNING: applied via the Vagrantfile:
WARNING: 
WARNING:   Vagrant.configure(2) do |config|
WARNING:     config.vm.provider :vmware_desktop do |vmware|
WARNING:       vmware.vmx["ethernet0.pcislotnumber"] = "160"
WARNING:     end
WARNING:   end
WARNING: 
WARNING: For more information: https://www.vagrantup.com/docs/vmware/boxes.html#vmx-allowlisting
==> default: Starting the VMware VM...
==> default: Waiting for the VM to receive an address...
==> default: Forwarding ports...
    default: -- 80 => 8080
    default: -- 22 => 2222
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Configuring network adapters within the VM...
==> default: Waiting for HGFS to become available...
==> default: Enabling and configuring shared folders...
    default: -- /Users/anonymous: /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
vagrant port
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.

    80 (guest) => 8080 (host)
    22 (guest) => 2222 (host)
curl -4 192.168.212.173
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
nginx.org.

Commercial support is available at
nginx.com.

 

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

I have installed a simple website in Ubuntu with the help of nginx, for example, to show how port forwarding works in a virtual machine.

 

+ Recent posts