Guest User

Untitled

a guest
Jul 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. var http = require('http');
  2. var app = require('./app');
  3. var express = require('express');
  4. var apps = express();
  5. var path = require('path');
  6.  
  7. apps.use(express.static(path.join(__dirname, 'public')));
  8. http.createServer(app.handleRequest).listen(8000);
  9.  
  10. var url = require('url');
  11. var fs = require('fs');
  12.  
  13. function renderHTML(path, response) {
  14. response.writeHead(200, { 'Content-Type': 'text/html' });
  15. fs.readFile(path, null, function(error, data) {
  16. if (error) {
  17. response.writeHead(404);
  18. response.write('File not found!');
  19. } else {
  20. response.write(data);
  21.  
  22. }
  23. response.end();
  24. });
  25. }
  26.  
  27. module.exports = {
  28. handleRequest: function(request, response) {
  29. var path = url.parse(request.url).pathname;
  30. switch (path) {
  31. case '/':
  32. renderHTML('./index.html', response);
  33. break;
  34. case '/login':
  35. renderHTML('./login.html', response);
  36. break;
  37. default:
  38. response.writeHead(404);
  39. response.write('Route not defined');
  40. response.end();
  41. }
  42. }
  43. };
  44.  
  45. <!doctype html>
  46. <html lang="en">
  47. <head>
  48. <meta charset="UTF-8">
  49. <title>Test Doc</title>
  50. <link rel="stylesheet" type="text/css" href="/css/style.css" />
  51. </head>
  52. <body>
  53. <p>Coba ku </p>
  54. <div id="container" style="height: 500px"></div>
  55. </body>
  56. </html>
Add Comment
Please, Sign In to add comment