无法获取/测试-ExpressJS

时间:2019-01-07 07:35:57

标签: javascript reactjs express

我想在用户输入此URL地址时加载一条日志消息:

http://localhost:3000/test

但是我收到此错误消息:无法GET / test

这是我的代码:

import express from 'express';
import React from 'react';
import { renderToString } from 'react-dom/server';
import App from './client/App';
import Html from './client/Html';
import { ServerStyleSheet } from 'styled-components'; // <-- importing ServerStyleSheet

var http = require("http");
var https = require("https");
const port = 3000;
const server = express();

const request = require('request');

server.get('/test', (req, res) => res.send('Hello  '));

server.get('/login', (req, res) => {

  const sheet = new ServerStyleSheet();

  const body = renderToString(sheet.collectStyles(<App />)); 
  const styles = sheet.getStyleTags(); 
  const title = 'Server side Rendering with Styled Components';
 console.log("hello!");
 request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log("body") 
  }
});

  res.send(
    Html({
      body,
      styles, 
      title
    })
  );
});

server.listen(port);
console.log(`Serving at http://localhost:${port}`);

这有什么问题?我想念什么?

我的组件版本:

“表达式”:“ ^ 4.14.0”, “ react”:“ ^ 16.2.0”, “ react-dom”:“ ^ 16.2.0”, “ request”:“ ^ 2.88.0”,

1 个答案:

答案 0 :(得分:1)

我会这样:

// init variables
var express = require('express');
var app = express();

// routes to the specified path with the specified callback functions.
app.get('/test', function (req, res) {
  res.send('Hello World!');
  console.log('Hello');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});
  

我想在用户输入此URL地址时加载一条日志消息:

这就是您所需要的。

获取更多信息here