[RUBY] How to set up a local development server

Introduction

I will explain as rudimentarily as possible how to set up a part of the file system on a local server and access it from a browser to check the operation of Web application development.

For Python

This is easy if you are in an environment where Python works. The Python standard library has a dedicated module that can be launched directly from the command line. The module names are different between Python 2 and 3, so I will explain each.

Python 2 uses Simple HTTP Server. If the port number is omitted, 8000 will be used.

$ python -m SimpleHTTPServer [port number (default is 8000)]

Python3 uses http.server.

$ python3 -m http.server [port number (default is 8000)]

Now, if you access http: // localhost: 8000 (or just localhost: 8000) from your browser, the server will start with the directory you started as the root. It responds to access to the directory as follows (same as the default behavior of the Apache server):

Exit the server with Ctrl-C.

For Ruby

Since Ruby 1.9.3, there is an httpd function in the Kernel module, which can be started from the command line as follows. The functionality is the same as Python.

$ ruby -run -e httpd . -p 8000

Actually, I didn't know this until now, and I found it in the related posts displayed immediately after uploading the first draft (I received a comment from udzura immediately after that. Thank you). But if you are a Ruby programmer, I still recommend the following method.

I'm a Ruby programmer and always use the Rack gem (Ruby before 1.9.3 is also possible). First, install Rack.

Rack is used by many Ruby frameworks. Especially if you have Ruby on Rails installed, you don't need to do this.

$ [sudo] gem install rack

sudo is required if you have Ruby installed in your system directory. (I use it) rbenv and RVM Not required if you are using the version switching tool.

Next, create a small file named config.ru in the directory where you start the server. The contents are only the following line.

config.ru


run Rack::Directory.new '.'

You are now ready. Finally, start it with the rackup command (the following is a start example). The default port number is 9292, which is rackup -p 8000 (example).

$ rackup
[2014-04-01 14:18:53] INFO  WEBrick 1.3.1
[2014-04-01 14:18:53] INFO  ruby 1.9.3 (2013-06-27) [i686-linux]
[2014-04-01 14:18:53] INFO  WEBrick::HTTPServer#start: pid=5814 port=9292

Note that a directory listing is always returned for access to a directory (even if index.html exists, it is listed as one of the files). This should be enough for development. Once set, all you have to do is rackup.

The advantage of Rack is that you can customize it as much as you like, giving you the flexibility to write Ruby code in config.ru for server-side programming. However, this is beyond the scope of this article, so detailed explanations are omitted.

The server side of my latest work "Weather Data Map" is also made with Rack. I would like to have the opportunity to explain Rack separately later.

For Node.js

I'm not very familiar with Node.js, but I know someone who makes a local server for Node.js, so I will introduce it (I also confirmed the operation). For details, please read the explanation on the following site.

https://github.com/shimizu/SimpleWebServer

I received a comment from ka_ immediately after posting. It seems that there are some node modules.

Ka_'s comments are well organized, so please see that for details.

For PHP

I received a comment from kimama1997 for the PHP version, so I will add it (PHP 5.4+). See the comments section for details.

$ php -S localhost:3000

By the way, I have experience with PHP, but I am not currently using it, and I have not installed it in my current development environment.

Recommended Posts

How to set up a local development server
Set up a local server with Go-File upload-
Set up a local server with Go-File download-
Set up a simple local server on your Mac
How to use App Engine's local development server dev_appserver.py
How to set up SVM using Optuna
How to set the server time to Japanese time
How to set up the development environment of ev3dev [Windows version]
How to set up a simple SMTP server that can be tested locally in Python
Set up a simple HTTPS server in Python 3
Set up a simple HTTPS server with asyncio
How to set up Random forest using Optuna
Send mail with mailx to a dummy SMTP server set up with python.
Set up a test SMTP server in Python.
Set up a UDP server in C language
How to set up a jupyter notebook on ssh destination (AWS EC2)
Set up a local web server in 30 seconds using python 3's http.server
[Note] How to create a Ruby development environment
Set up a simple SMTP server in Python
Set up a Python development environment on Marvericks
[Development environment] How to create a data set close to the production DB
[Note] How to create a Mac development environment
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Set up a development environment for natural language processing
Set up a Minecraft resource (Spigot) server via docker (2)
Set up a file server on Ubuntu 20.04 using Samba
Set up a free server on AWS in 30 minutes
[Part 1] Let's set up a Minecraft server on Linux
How to create a local repository for Linux OS
How to build a development environment for TensorFlow (1.0.0) (Mac)
How to create a simple TCP server / client script
Set up a Minecraft resource (Spigot) server via docker
How to run Django on IIS on a Windows server
How to set up and compile your Cython environment
Set up a Python development environment with Sublime Text 2
[Vagrant] Set up a simple API server with python
How to call a function
How to set up a Google Colab environment with Coursera's advanced machine learning courses
How to set up a VPN gateway to establish a connection between Alibaba Cloud and AWS
I want to set up a mock server for python-flask in seconds using swagger-codegen.
How to use GitHub on a multi-person server without a password
Set up a Python development environment with Visual Studio Code
How to import Python library set up in EFS to Lambda
Set up a web server with CentOS7 + Anaconda + Django + Apache
How to specify a public directory Python simple HTTP server
[Python Kivy] How to create a simple pop up window
How to build a python2.7 series development environment with Vagrant
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
I want to set up a GUI development environment with Python or Golang on Mac
How to write a Python class
How to make a slack bot
How to create a Conda package
How to make a crawler --Advanced
How to make a recursive function
How to create a virtual bridge
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
How to delete a Docker container
How to speed up Python calculations
How to make a crawler --Basic