HTTP Module

This guide demonstrates the basics of creating a C++ web service that processes incoming HTTP requests.

  1. On the Project menu, select the Add New Item...
  2. In the Add New Item dialog, select the Visual C++ and then select Header File (.h)
  3. Enter the Name HelloWebWorld and click the Add button.
  4. Add the following code to the file:
    #pragma once
    #include "tef/Tentity.h"
    
    class HelloWebWorld : public tef::RequestHandler
    {
    public:
    	HelloWebWorld();
    	virtual ~HelloWebWorld();
    	virtual const char* Path() const override;
    	virtual void Process(tef::RequestContext& context) const override;
    };
                    
  5. On the Project menu, select the Add New Item...
  6. In the Add New Item dialog, select the Visual C++ and then select C++ File (.cpp)
  7. Enter the Name HelloWebWorld and click the Add button.
  8. Add the following code to the file:
    #include "stdafx.h"
    #include "HelloWebWorld.h"
    
    static HelloWebWorld Instance;
    
    HelloWebWorld::HelloWebWorld()
    {
    	tef::Register(this);
    }
    
    HelloWebWorld::~HelloWebWorld()
    {
    }
    
    const char* HelloWebWorld::Path() const
    {
    	return "/helloworld";
    }
    
    void HelloWebWorld::Process(tef::RequestContext& context) const
    {
    	tef::JsonOutput<128> json(context.Output);
    	json.startObject();
    	json.writeStringProperty(L"Message", L"Hello World!");
    	json.endObject();
    }
                    
  9. The default configuration (default.app) is set listening to port 42580 which must be registered on the first run:
    1. Locate the addurl.bat file in the tef_sdk directory.
    2. Edit the user name in the addurl.bat file
    3. Execute the addurl.bat file with the administrator privileges.

    Note: the default.app is a text file:
    [address]
    http://+:42580/
                    
    [address] : the syntax is explained here.
  10. Compile (F7) and run the project (F5). It starts the tefsvr.exe.
  11. In a browser, enter http://127.0.0.1:42580/helloworld. The following image depicts the output:

  12. Use Ctrl+C in the command prompt window to stop the server.
  13. Next Parallel Fibonacci

See Also:

C++ API Reference

Table of Contents